Objective c 更改UIStepper的外观
UIStepper非常方便,但我想改变它的外观,我需要自定义图标而不是加号和减号,我还想改变控件的颜色。我怎样才能做到这一点?提前谢谢 您目前无法这样做。如果要自定义图标,您必须编写自定义Objective c 更改UIStepper的外观,objective-c,ios,ios5,Objective C,Ios,Ios5,UIStepper非常方便,但我想改变它的外观,我需要自定义图标而不是加号和减号,我还想改变控件的颜色。我怎样才能做到这一点?提前谢谢 您目前无法这样做。如果要自定义图标,您必须编写自定义UIControl对象 从中,您只能更改以下参数: continuous property autorepeat property wraps property minimumValue property maximumValue property stepValue pro
UIControl
对象
从中,您只能更改以下参数:
continuous property
autorepeat property
wraps property
minimumValue property
maximumValue property
stepValue property
value property
您不能自定义图标
但是,由于它是UIView
的一个子类,您可以尝试更改背景色
此操作有效:
[[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];
但不确定它是如何证明未来的。从iOS 6.0开始,您可以使用
-(UIImage*)递减forState:(UIControlState)state
和-(UIImage*)递增ImageforState:(UIControlState)state
更改UIStepper控件上的标签
这适用于iOS 6.0:
[[u stepper SetDecrementImageName:@“decrementIcon.png”]用于状态:UIControlStateNormal];
您也可以单步浏览UIView并进行猜测。无论如何,这是我的代码,它不直接回答问题,但有点简洁
@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
这是一个适合我的解决方案 平台iOS 7.1
[stepper setBackgroundImage:[UIImage new] forState:UIControlStateNormal];
[stepper setDividerImage:[UIImage new] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal];
结果只有递增和递减图像可见。以下是Swift 4.0版本:
stepper.setDecrementImage(UIImage(named: "yourDecrementImage.png"), for: UIControlState.normal)
stepper.setIncrementImage(UIImage(named: "yourIncrementImage.png"), for: UIControlState.normal)
我还发现缩小自定义图像以适应步进按钮会使它们模糊。这是我发现的唯一一个可以适当缩小图像而不模糊的扩展功能:
extension UIImage {
func resize(targetSize: CGSize) -> UIImage {
return UIGraphicsImageRenderer(size:targetSize).image { _ in
self.draw(in: CGRect(origin: .zero, size: targetSize))
}
}
}
更改
backgroundColor
将不起作用,因为决定了如何实现UIStepper
。发问者应该提交一个bug:我知道这很糟糕,不管怎样,有开源的替代方案吗?顺便说一下,谢谢你的回答。这是不可能的。请提交一个请求此功能的bug:nicefind。有没有办法为左控件和右控件设置不同的图像?@AndreiStoleru我假设您的目标是ios5,是指+递增和-递减图像。除非在UIStepper上子类化或添加一个类别。但您可以使用[[UIImageView AppearanceWhen containedIn:[UIStepper class],nil]setImage:[UIImageName:@“divider.png]”自定义分割器<在iOS 9中,code>AppearanceWhen contained in已被弃用,并且不再建议使用它,以防将来的人来到这里。工作正常。如果在iOS上>=6,这应该是可接受的答案。