改变UIStepper的外观

UIStepper是非常方便的,但我想改变它的外观,我需要定制的图标不是加号和减号,我也想改变控制的颜色。 我怎样才能做到这一点? 提前致谢!

你现在不能这样做。 如果要自定义图标,则必须编写自定义的UIControl对象。

从UIStepper类参考 ,你可以改变的唯一参数是

  continuous property autorepeat property wraps property minimumValue property maximumValue property stepValue property value property 

您无法自定义图标。

但是,由于它是UIView的子类,所以可以尝试更改backgroundColor

从iOS 6.0开始,您可以使用- (UIImage *)decrementImageForState:(UIControlState)state- (UIImage *)incrementImageForState:(UIControlState)state来更改UIStepper控件上的标签。

这适用于iOS 6.0:

[_stepper setDecrementImage:[UIImage imageNamed:@"decrementIcon.png"] forState:UIControlStateNormal];

这工作:

 [[UIButton appearanceWhenContainedIn:[UIStepper class], nil] setBackgroundImage:[UIImage imageNamed:@"normal.png"] forState:UIControlStateNormal]; [[UIButton appearanceWhenContainedIn:[UIStepper class], nil] setBackgroundImage:[UIImage imageNamed:@"highlighted.png"] forState:UIControlStateHighlighted]; [[UIButton appearanceWhenContainedIn:[UIStepper class], nil] setBackgroundImage:[UIImage imageNamed:@"disabled.png"] forState:UIControlStateDisabled]; 

不知道未来如何certificate。

这里是适合我的解决scheme。

平台iOS 7.1

 [stepper setBackgroundImage:[UIImage new] forState:UIControlStateNormal]; [stepper setDividerImage:[UIImage new] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal]; 

结果只是递增,递减图像是可见的。

您可以随时浏览UIViews并猜测。 无论如何,这是我的代码,它不直接回答这个问题,但是很整齐。

 @implementation UIStepper (Util) - (void) fixForIos7 { if (!IS_IOS7) return; for (UIView *view in self.subviews) { view.backgroundColor = [UIColor whiteColor]; UIButton *button = (UIButton*)view; [button setTintColor:[UIColor blackColor]]; } } @end