iOS 6 UISegmentedControl与iOS 7devise
我正在开发一个应用程序,它应该可以在iOS 6和iOS 7上运行,并且具有相同的平面devise。
我试图自定义我的UISegmentedControl有边界,圆angular半径和所有,但我无法弄清楚如何做到这一点。 到目前为止,我只有一个平坦的背景。
有没有人有任何build议有一个iOS 6 UISegmentedControl看起来像一个iOS 7吗?
编辑:
我想拥有
代替
你可以使用下面的代码:
// To set colour of text NSDictionary *attributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor]; [segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal]; NSDictionary *highlightedAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor]; [segmentedControl setTitleTextAttributes:highlightedAttributes forState:UIControlStateHighlighted]; // Change color of selected segment segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar; UIColor *newTintColor = [UIColor colorWithRed: 255/255.0 green:100/255.0 blue:100/255.0 alpha:1.0]; segmentedControl.tintColor = newTintColor; UIColor *newSelectedTintColor = [UIColor clearColor]; [[[segmentedControl subviews] objectAtIndex:0] setTintColor:newSelectedTintColor];
而对于圆angular你可以使用下面的代码:
// Add rounded yellow corner to segmented controll view [segmentedControl.layer setCornerRadius:4.0f]; [segmentedControl.layer setBorderColor:[UIColor colorWithRed:1.0 green:0.7 blue:0.14 alpha:1.0].CGColor]; [segmentedControl.layer setBorderWidth:1.5f]; [segmentedControl.layer setShadowColor:[UIColor blackColor].CGColor]; [segmentedControl.layer setShadowOpacity:0.8]; [segmentedControl.layer setShadowRadius:3.0]; [segmentedControl.layer setShadowOffset:CGSizeMake(2.0, 2.0)];
你可以看看这里的一个例子,或者采取一个自定义的控制,并改变其图像,以适应您的需要。
下面是一个如何可以UISegmentedControl
的例子:
@implementation CustomSegmentedControl -(id)initWithItems:(NSArray *)items { self = [super initWithItems:items]; if (self) { [self setDividerImage:[UIImage imageNamed:@"segmented-control-divider-none-selected"] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; [self setDividerImage:[UIImage imageNamed:@"segmented-control-divider-left-selected"] forLeftSegmentState:UIControlStateSelected rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; [self setDividerImage:[UIImage imageNamed:@"segmented-control-divider-right-selected"] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault]; // Set background images UIImage *normalBackgroundImage = [UIImage imageNamed:@"segmented-control-normal"]; [self setBackgroundImage:normalBackgroundImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; UIImage *selectedBackgroundImage = [UIImage imageNamed:@"segmented-control-selected"]; [self setBackgroundImage:selectedBackgroundImage forState:UIControlStateSelected barMetrics:UIBarMetricsDefault]; [self setBackgroundImage:selectedBackgroundImage forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault]; double dividerImageWidth = [self dividerImageForLeftSegmentState:UIControlStateHighlighted rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault].size.width; [self setContentPositionAdjustment:UIOffsetMake(dividerImageWidth / 2, 0) forSegmentType:UISegmentedControlSegmentLeft barMetrics:UIBarMetricsDefault]; [self setContentPositionAdjustment:UIOffsetMake(- dividerImageWidth / 2, 0) forSegmentType:UISegmentedControlSegmentRight barMetrics:UIBarMetricsDefault]; return self; }
我终于使用了这个子模块。 它做的工作: