我将自定义图像用于UISegmentedControl
的正常/选定状态。
当我点击已选择的细分时,它的外观会更改为突出显示。
我尝试为突出显示的+选定状态设置图片,但忽略了它:
[segmentedControl setBackgroundImage:appearance.selectedBackground
forState:UIControlStateSelected | UIControlStateHighlighted
barMetrics:UIBarMetricsDefault];
是否有任何方式(包括非破坏性的私有)在选定的状态再次点击时将其置于选定状态?
答案 0 :(得分:5)
我知道现在已经晚了但是对于那些有同样问题的人: 您必须为每个状态设置所有dividerImages
// Set background images
UIImage *normalBackgroundImage = [UIImage imageNamed:@"segmented-background-unselected"];
UIImage *selectedBackgroundImage = [UIImage imageNamed:@"segmented-background-selected"];
[self setBackgroundImage:normalBackgroundImage
forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self setBackgroundImage:selectedBackgroundImage
forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[self setBackgroundImage:normalBackgroundImage
forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
[self setBackgroundImage:selectedBackgroundImage
forState:UIControlStateSelected | UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
// Set divider images
UIImage *buttonImageLeftSelected = [UIImage imageNamed:@"segmented-left-selected"];
UIImage *buttonImageNoneSelected = [UIImage imageNamed:@"segmented-none-selected"];
UIImage *buttonImageRightSelected = [UIImage imageNamed:@"segmented-right-selected"];
[self setDividerImage:buttonImageNoneSelected
forLeftSegmentState:UIControlStateNormal
rightSegmentState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[self setDividerImage:buttonImageLeftSelected
forLeftSegmentState:UIControlStateSelected
rightSegmentState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[self setDividerImage:buttonImageLeftSelected
forLeftSegmentState:UIControlStateSelected | UIControlStateHighlighted
rightSegmentState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[self setDividerImage:buttonImageLeftSelected
forLeftSegmentState:UIControlStateSelected
rightSegmentState:UIControlStateHighlighted
barMetrics:UIBarMetricsDefault];
[self setDividerImage:buttonImageRightSelected
forLeftSegmentState:UIControlStateHighlighted
rightSegmentState:UIControlStateSelected
barMetrics:UIBarMetricsDefault];
[self setDividerImage:buttonImageRightSelected
forLeftSegmentState:UIControlStateNormal
rightSegmentState:UIControlStateSelected | UIControlStateHighlighted
barMetrics:UIBarMetricsDefault];
[self setDividerImage:buttonImageRightSelected
forLeftSegmentState:UIControlStateNormal
rightSegmentState:UIControlStateSelected
barMetrics:UIBarMetricsDefault];
答案 1 :(得分:1)
这很简单,对我来说很完美,但你需要子类化并覆盖touchesBegan
方法:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UIControl *control in self.subviews) {
if ([control respondsToSelector:@selector(isSelected)] && control.isSelected) {
// found our selected segment, now check if we actually tapped it
UITouch *touch = [touches anyObject];
if (CGRectContainsPoint(control.frame, [touch locationInView:self])) {
// tapping selected segment, do nothing. this avoids highligting it
return;
}
}
}
[super touchesBegan:touches withEvent:event];
}