heshammegid / hmsegmentedcontrol Goto Github PK
View Code? Open in Web Editor NEWA highly customizable drop-in replacement for UISegmentedControl.
License: Other
A highly customizable drop-in replacement for UISegmentedControl.
License: Other
I think the icons should be at the left of the labels... not in the middle.
_segmentedControl = [[HMSegmentedControl alloc]
initWithSectionImages:@[[UIImage imageNamed:@"place_25"],[UIImage imageNamed:@"directions_16"]]
sectionSelectedImages:@[[UIImage imageNamed:@"place_25"],[UIImage imageNamed:@"directions_16"]]
titlesForSections:@[@"Place"),@"Directions"]];
_segmentedControl.frame = CGRectMake(0.0f, 0.0f, frame.size.width, 40.0f);
_segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleFullWidthStripe;
_segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
_segmentedControl.selectionIndicatorColor = [UIColor CycleMapColor];
_segmentedControl.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor cm_veryDarkGrayColor],
NSFontAttributeName : [UIFont CycleMapBoldFontOfSize:12.0f]};
_segmentedControl.selectedTitleTextAttributes = @{NSForegroundColorAttributeName : [UIColor CycleMapColor]};
I have to set HMSegmentedControl with the 1/2 width of the whole screen, using auto layout. (iphone 5, 6 and 6 plus).
So the width will change according to the screen. and i have fixed sizes of the images.
How to set the button width programmatically and center this images in the center of the buttons
NSArray *iconsLeftMenu = [[NSArray alloc]initWithObjects:
[UIImage imageNamed:@"icon-timeline"],
[UIImage imageNamed:@"icon-map"],
nil];
segmentedControl = [[HMSegmentedControl alloc]
initWithSectionImages:iconsLeftMenu
sectionSelectedImages:iconsLeftMenu];
segmentedControl.frame = CGRectMake(0, 300, 320, 50);
// Bellow is the width of the HMSegmentedControl
CGRect frameMenu = headerLeftView.bounds;
segmentedControl.frame = frameMenu;
segmentedControl.backgroundColor = [UIColor clearColor];
segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationNone;
segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleBox;
[segmentedControl addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
[headerLeftView addSubview:segmentedControl];
Love this flat style segment control, but I want to layout the segment buttons in the vertical direction.
I am using the cocoapod and trying to change the opacity of they indicator box. I have changed its color and opacity to 1.0 but it is giving me a color darker than the one I wanted. I also tried going into the HMSegmentedControl.m and changing the value from the commonInit but it is still not working. Any help would be appreciated.
I want to change each segment to change the text, how can i achieve this. in the current there is a lot of white space if the text is short?
Best regards
Hi,
I saw you took extra care in multiple instances to avoid using -[NSString sizeWithAttributes:]
as to maximise compatibility with older iOS versions. I found a place though where this has been done differently and becomes an issue on iOS 5.1: HMSegmentedControl.m
lines 226 and 227. The symbol NSFontAttribueName
doesn't exist on this older version of iOS5 and it makes the app crash on startup because it cannot resolve the external symbol. If you agree to solve this I can recommend those two solutions:
replacing NSFontAttributeName
with kCTFontAttributeName
since they both have the same value
using the same macro you do on lines 510, 521, 537, 549:
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
Thanks for considering!
saw you just posted a number of changes, you should bump the podspec version so changes are properly detected for those of us using cocoapods :-)
Hi,
Since the release of 1.3 and the introduction of the scrolling capability we're not able to use HMSegmentedControl
in combination with auto layout's constraints.
We configure our subviews in viewDidLoad
and at that time their frame is not determined yet. We use auto layout constraints to have the subviews automatically placed properly.
The problem is that when HMSegmentedControl
sets the frame of self.scrollView
(line 257), self.frame.size.width
and self.frame.size.height
are equal to 0
making the view invisible.
Here's an example of our setup:
- (void)viewDidLoad
{
[super viewDidLoad];
HMSegmentedControl *segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:sectionTitles];
segmentedControl.backgroundColor = [UIColor clearColor];
segmentedControl.textColor = [UIColor grayColor];
segmentedControl.font = [UIFont boldSystemFontOfSize:16.0f];
segmentedControl.selectedTextColor = [UIColor blackColor];
segmentedControl.selectionIndicatorHeight = 5.0f;
segmentedControl.selectionIndicatorColor = [UIColor colorWithHex:0x00A5E4];
segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleBox;
segmentedControl.selectedSegmentIndex = 0;
[self.containerView addSubview:segmentedControl];
[self.containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-0-[segmentedControl]-0-|"
options:0
metrics:nil
views:@{@"segmentedControl": segmentedControl}]];
[self.containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[segmentedControl]-0-|"
options:0
metrics:nil
views:@{@"segmentedControl": segmentedControl}]];
}
Note: self.containerView
is already a subview of self.view
through the storyboard
When using this control with other events, sometimes the touches don't register. Possible change
Changing :
UITouch *touch = [[event allTouches] anyObject];
to
UITouch *touch = [touches anyObject];
fixed the issue but haven't tested properly.
I am using the segmented control with images and a scroll view (segmented control 4 in the demo) and everything is working perfectly except when i use a navigation controller, the area of the segmented control seem to have bigger content are and i can scroll vertically and move around the segmented control items
Hey.
Im using HMSegmentedControl on view with UIScrollView that scrolls content of the view. Everything is working great except I can't scroll view if touch starts from segmented control.
I think segmented control is receiving scroll event but, as it has no use for it, it just ignores it. Is there any way to forward scroll event from HMSegmentedControl to its superview?
Was wondering why I couldn't set background to [UIColor clearColor].
Realized I'd missed setting opaque to NO.
It would be nice to have a multiple segment selection option
HMSegmentetControl.m crashes in drawRect: on the line titleLayer.frame = rect;
when running on iOS 7 preview 2. It seems that rect.origin.x = nan and rect.size.width = inf.
The error message is:
'CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [nan 17]'
Although my app built & ran without issue on iOS simulator and on my local device using HMSegmentedControl, the app crashed upon launching when the .ipa file was distributed via TestFlightApp.
Turns out the message call 'setSelectedSegmentIndex:0' to the segmented control in [viewDidLoad] was triggering the crash after downloading from TestFlightApp.
Commented that line out and app (& segmented control) functioned fine. Strange.
hi
how to do it to show a loop.
Thank you
Here is what i am trying to achieve (see the top bar):
https://d30r23kyhrlyhs.cloudfront.net/assets/home/fg_iphone_1280h-8a1630cb0a2632a34d999d62fa832dba.jpg
Infact it was said this app using your control.
Thank you
hi,my friend.I notice your Control last year on github and it helps me a lot. thank you for sharing,i like it very much.
i think it can be much better if it has better Interaction design effect. just like NYSegmentedControl on github. while the scrollView didScroll in HMSegmentedControl,the indicatorView changed at once.hm,may be a little difficult to understand....I am sorry that my written English is weak.
haha, thank you for reading my issue.
I was just trying to get the control to work in a storyboard and I started with an existing UISegmentedControl, then tried to change the classname to HMSegmentedControl, but Interface Builder doesn't seem to like it.
Any suggestions ?
Thanks
Chris
like subject
plz help!
I`m testing in a Iphone 4.
If I just tap the area of the segment, the method touchesBegan is not called. I have to do a top of a certain duration to get the method called.
hi,
cool control - good work - thanks!
it was working great when used with images only i.e. it was taking actual image width and enabled horizontal scrolling but when also adding titles, it fits everything into frame width and not allow for scrolling - i set scrollenabled YES and
segmentWidthStyle = HMSegmentedControlSegmentWidthStyleFixed;
etc. but inside code
sectionWidth = MAX(stringWidth, imageWidth)
is not respected and the calculation is off.
in the method
could you plz check
thanks
I am Newbie in iOS Development.I use HMSegmentedControll For my Application it Contain Seven Section Title and i want to Change BackGround Colour of my SegmentedControll When it was Swipe to Next Title How it Possible. If it is Possible then Please Give me Solution?
Environment: Ipad3, ios7
: CGContextSetStrokeColorWithColor: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
I had to create this category, so it won't interfere with scrollsToTop behaviour of other scroll views being displayed on screen, such as a UITableView.
@implementation HMSegmentedControl (ScrollView)
- (void)disableScrollsToTop
{
[self disableScrollsToTopForViewAndSubviews:self];
}
- (void)disableScrollsToTopForViewAndSubviews:(UIView *)view
{
for (UIView *subview in [view subviews]) {
if ([subview isKindOfClass:[UIScrollView class]]) {
UIScrollView *scrollView = (UIScrollView *)subview;
[scrollView setScrollsToTop:NO];
}
[self disableScrollsToTopForViewAndSubviews:subview];
}
}
@end
I can fork and pull request, but I don't think it's necessary in this case. How about setting the self.scrollView.scrollsToTop = NO
?
backgroundcolor is not enough
Would like to be able to set a flag that enables determine if the touchesEnded method
will call the setSelectedSegmentIndex method even if the you are touching the already selected index.
Believe it can be achieved by adding a new boolean property and checking it. see below.
(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:self];
if (CGRectContainsPoint(self.bounds, touchLocation)) {
NSInteger segment = (touchLocation.x + self.scrollView.contentOffset.x) / self.segmentWidth;
// change this to
//if (segment != self.selectedSegmentIndex) {
// [self setSelectedSegmentIndex:segment animated:YES notify:YES];
//}
// to this
if (okToTouchSelectedIndex) {
[self setSelectedSegmentIndex:segment animated:YES notify:YES];
}
}
}
Hello,
I would like to enable horizontal scrolling anywhere in my view to change selected index of the HMSegmentedControl. Is it possible ?
Thank you
PS: Sorry I'm not english
I drag and drop a View on ViewController,Added a IBOutlet,use code:
[self.segmentControl setSectionTitles:@[@"Worldwide", @"Local", @"Headlines",@"aav"]];
// [self.segmentControl setSelectedSegmentIndex:1];
[self.segmentControl setBackgroundColor:[UIColor colorWithRed:0.7 green:0.7 blue:0.7 alpha:1]];
[self.segmentControl setTextColor:[UIColor whiteColor]];
[self.segmentControl setSelectedTextColor:[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:1]];
[self.segmentControl setSelectionIndicatorColor:[UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1]];
[self.segmentControl setSelectionStyle:HMSegmentedControlSelectionStyleBox];
[self.segmentControl setSelectionIndicatorLocation:HMSegmentedControlSelectionIndicatorLocationUp];
It doesn't work, how to solve?
can you set a property that a Seperator line at the bottom of segment (which we can set the Seperator line is hidden and the color of the Seperator line)?
Hello, I'm expiriencing crashes with following error:
*** -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2]
the stack trace is here:
0 CoreFoundation <redacted> + 130
1 libobjc.A.dylib objc_exception_throw + 38
2 CoreFoundation <redacted> + 176
3 VLPush -[HMSegmentedControl frameForSelectionIndicator] (HMSegmentedControl.m:233) + 3038043
4 VLPush -[HMSegmentedControl setSelectedSegmentIndex:animated:notify:] (HMSegmentedControl.m:392) + 3041749
5 VLPush -[HMSegmentedControl touchesEnded:withEvent:] (HMSegmentedControl.m:315) + 3040615
6 VLPush -[HMScrollView touchesEnded:withEvent:] (HMSegmentedControl.m:51) + 3033051
7 UIKit <redacted> + 528
8 UIKit <redacted> + 758
9 UIKit <redacted> + 196
10 UIKit <redacted> + 7102
11 CoreFoundation <redacted> + 14
12 CoreFoundation <redacted> + 206
13 CoreFoundation <redacted> + 622
14 CoreFoundation CFRunLoopRunSpecific + 522
15 CoreFoundation CFRunLoopRunInMode + 106
16 GraphicsServices GSEventRunModal + 138
17 UIKit UIApplicationMain + 1136
18 VLPush main (main.m:16) + 144487
19 libdyld.dylib <redacted> + 2
my use case is always the same, I have 3 tabs. Any help is appreciated!
Have a nice day
I am using HMSegmentedControl as an IBOutlet in a UIView. Therefore, I cannot programmatically init the control, which seems to be the only way to set the images for the segments. I have tried everything I can think of to set the segments after the control has been initialized, but it seems that it must be done on initialization. Is there another way that you have provided?
Segmented control with images not run on ios 7.1. Do you help fix it soon?
Is it possible to add selectedFont property just like selectedTextColor when a segment gets selected?
An older version gets installed by adding the pod line to the Podfile,
Please check!
Latest release was dated last Nov, and the current code is updated this May (including sample).
Hello, I have a problem with selectedSegmentIndex, since for more than change the value to 0 or 2, for example, always shows me the contents of one selectedSegmentIndex
Do not understand why ...
self.segmentedControl4 = [[HMSegmentedControl alloc] initWithFrame:CGRectMake(53, 300 + yDelta, 918, 50)];
self.segmentedControl4.sectionTitles = @[@"Información", @"Trailer", @"Comentarios", @"Enlaces"];
self.segmentedControl4.selectedSegmentIndex = 0;
self.segmentedControl4.backgroundColor = [UIColor clearColor];
In the example also HMSegmentedControlExample.xcodeproj so with the segmentedControl4.
Thanks for any help.
your segmented control is so beautiful but I need to place the first and the last elements in the middle when they are selected.
It seems it is enough easy for you to add this as an additional option
the title ,i want to make it wrap,help me? thank you
Say you have two segments. It is possible in the simulator, when you click on the rightmost pixel of the control, to have it setSelectedIndex to 3. I'm not especially familiar with the code base, but I was able to prevent this behavior by checking whether the selected segment number is greater than [sectionTitles count].
HMSegmentedControl.m
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
...
if(segment < [self.sectionTitles count])
{
if (segment != self.selectedSegmentIndex) {
[self setSelectedSegmentIndex:segment animated:YES notify:YES];
}
}
...
}
➜ ~ pod search HMSegmentedControl
-> HMSegmentedControl (1.3.0)
A drop-in replacement for UISegmentedControl mimicking the style of the one
in Google Currents and various other Google products.
pod 'HMSegmentedControl', '~> 1.3.0'
- Homepage: https://github.com/HeshamMegid/HMSegmentedControl
- Source: https://github.com/HeshamMegid/HMSegmentedControl.git
- Versions: 1.3.0, 1.2.0, 1.1.0, 1.0.0 [master repo]
➜ ~ pod --version
0.33.1
Any ideas? Thanks in advance:]
Sorry for not creating a proper pull request.
Current code [line 166]:
CGFloat y = roundf(((CGRectGetHeight(self.frame) - self.selectionIndicatorHeight) / 2) + (self.selectionIndicatorHeight - stringHeight / 2));
should be:
CGFloat y = roundf(((CGRectGetHeight(self.frame) - self.selectionIndicatorHeight) / 2) + (self.selectionIndicatorHeight - stringHeight) / 2);
note the closing ')'
This line causing crash in iOS 6 simulator, because following code is wrong (__IPHONE_7_0 is defined, but it doesn't mean that iOS7 is used!)
#ifdef __IPHONE_7_0
CGFloat stringWidth = [titleString sizeWithAttributes:@{NSFontAttributeName: self.font}].width + self.segmentEdgeInset.left + self.segmentEdgeInset.right;
#else
CGFloat stringWidth = [titleString sizeWithFont:self.font].width + self.segmentEdgeInset.left + self.segmentEdgeInset.right;
#endif
I change it too this
#define IS_OS_7_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
CGFloat stringWidth;
if (IS_OS_7_OR_LATER)
{
stringWidth = [titleString sizeWithAttributes:@{NSFontAttributeName: self.font}].width + self.segmentEdgeInset.left + self.segmentEdgeInset.right;
}
else
{
stringWidth = [titleString sizeWithFont:self.font].width + self.segmentEdgeInset.left + self.segmentEdgeInset.right;
}
I got a warning of "Code will never be executed" in .m file, line 535, while using in Swift .
} else if (self.type == HMSegmentedControlTypeTextImages && HMSegmentedControlSegmentWidthStyleFixed){
I wonder should it be like this:
} else if (self.type == HMSegmentedControlTypeTextImages && self.segmentWidthStyle == HMSegmentedControlSegmentWidthStyleFixed){
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.