2012-01-06 112 views
5

UIStepper非常方便,但我想改變它的外觀,我需要定製的圖標不是加號和減號,也是我想更改控件的顏色。我怎樣才能做到這一點?提前致謝!更改UIStepper的外觀

+0

這是不可能的。請提交一個請求此功能的bug:https://bugreport.apple.com – 2012-01-06 22:07:49

回答

2

您目前無法做到這一點。如果要自定義圖標,則必須編寫自定義的UIControl對象。

UIStepper Class Reference,您可以更改的唯一參數是

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

    value property 

您不能自定義圖標。

但是,由於它是UIView的子類,因此您可以嘗試更改backgroundColor

+0

由於決定如何實現UIStepper,所以更改'backgroundColor'將不起作用。提問者應該提交一個錯誤:https://bugreport.apple.com – 2012-01-06 21:54:33

+0

我發現這很糟糕,無論如何有沒有開源的選擇?謝謝你的答案。 – 2012-01-07 03:53:32

5

這工作:

[[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]; 

不知道它是證明,雖然未來如何。

+0

很好找。任何方式爲左側和右側控件設置不同的圖像? – tagyro 2012-10-18 09:18:05

+0

@AndreiStoleru我假設你的目標是ios5並且意味着+增量和減量圖像。無需在UIStepper上創建子類或添加類別。但你可以使用[[UIImageView appearanceWhenContainedIn:[UIStepper class],nil]來自定義分隔符:setImage:[UIImage imageNamed:@「divider.png」]]; – Timmehmainframe 2012-10-19 12:23:49

+0

「appearanceWhenContainedIn'已在iOS 9中棄用,不再推薦使用,以防未來的人員到達此處。 – hvaughan3 2017-04-06 19:46:30

13

從iOS 6.0開始,您可以使用- (UIImage *)decrementImageForState:(UIControlState)state- (UIImage *)incrementImageForState:(UIControlState)state來更改UIStepper控件上的標籤。

這適用於iOS 6.0:

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

+1

工作正常。如果在iOS> = 6上,這應該是被接受的答案。 – 2014-04-19 12:11:23

0

您可以通過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 
2

這裏是適合我的解決方案。

平臺的iOS 7.1

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

,結果只有遞增,遞減的圖像是可見的。