按鈕在 selected 為 YES 時,再次點擊或長按,按鈕的文字變成 normal 狀態(tài)的了。
代碼:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(50, 100, 100, 40);
button.backgroundColor = [UIColor blackColor];
[button setTitle:@"normal" forState:UIControlStateNormal];
[button setTitle:@"selected" forState:UIControlStateSelected];
button.selected = YES;
[self.view addSubview:button];
效果:

首先看一下UIButton的UIControlState的枚舉
typedef NS_OPTIONS(NSUInteger, UIControlState) {
UIControlStateNormal = 0,
UIControlStateHighlighted = 1 << 0, // used when UIControl isHighlighted is set
UIControlStateDisabled = 1 << 1,
UIControlStateSelected = 1 << 2, // flag usable by app (see below)
UIControlStateFocused NS_ENUM_AVAILABLE_IOS(9_0) = 1 << 3, // Applicable only when the screen supports focus
UIControlStateApplication = 0x00FF0000, // additional flags available for application use
UIControlStateReserved = 0xFF000000 // flags reserved for internal framework use
};
按鈕的狀態(tài)是位枚舉,不是普通的枚舉,也就是說按鈕可以有不止一個狀態(tài)。所以當按鈕的selected為YES時,再點擊的時候狀態(tài)應(yīng)該不是 normal 狀態(tài)而是其他狀態(tài)。
發(fā)現(xiàn)了相關(guān)的說明:UIButton control states
So why would you care about this? Say you were implementing an edit button or
a selection button with text, a background image and an image. You want different
text and images for the selected and unselected states (e.g. Editand Done),
but you also want to modify the images or background images when highlighting -
if you’re creating your own theme for the app then the default darkening or
dimming might not be what you want.
So you actually need four images - normal, highlighted, selected
and selected + highlighted.
This is achieved like so:
[self.button setImage:normal forState:UIControlStateNormal];
[self.button setImage:highlighted forState:UIControlStateHighlighted];
[self.button setImage:selected forState:UIControlStateSelected];
[self.button setImage:selectedHighlighted forState:UIControlStateSelected | UIControlStateHighlighted];
也就是說按鈕其實有兩類,四種狀態(tài):
1、在selected == NO的時候,有兩種分別對應(yīng) UIControlStateNormal 和 UIControlStateHighlighted
2、在selected == YES的時候,有兩種分別對應(yīng) UIControlStateSelected 和 UIControlStateSelected | UIControlStateHighlighted
由以上的說明,出現(xiàn)"按鈕在selected為YES時,再次點擊按鈕的狀態(tài)變成了normal狀態(tài)"情況的原因:
沒有設(shè)置 UIControlStateSelected | UIControlStateHighlighted 的狀態(tài),系統(tǒng)默認會顯示 UIControlStateNormal 的狀態(tài)
所以需要添加這個狀態(tài)的設(shè)置:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(50, 100, 100, 40);
button.backgroundColor = [UIColor blackColor];
[button setTitle:@"normal" forState:UIControlStateNormal];
[button setTitle:@"selected" forState:UIControlStateSelected];
// 添加此狀態(tài)
[button setTitle:@"selected" forState:UIControlStateSelected | UIControlStateHighlighted];
button.selected = YES;
[self.view addSubview:button];
最后:
支持這種寫法的方法 setXXX: forState:
- (void)setTitle:(nullable NSString *)title forState:(UIControlState)state; // default is nil. title is assumed to be single line
- (void)setTitleColor:(nullable UIColor *)color forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // default if nil. use opaque white
- (void)setTitleShadowColor:(nullable UIColor *)color forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // default is nil. use 50% black
- (void)setImage:(nullable UIImage *)image forState:(UIControlState)state; // default is nil. should be same size if different for different states
- (void)setBackgroundImage:(nullable UIImage *)image forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // default is nil
- (void)setAttributedTitle:(nullable NSAttributedString *)title forState:(UIControlState)state NS_AVAILABLE_IOS(6_0); // default is nil. title is assumed to be single line
原文鏈接:iOS系統(tǒng)控件UIButton的狀態(tài)--你不知道的小秘密
位移位運算:
如 UIViewAutoresizingFlexibleHeight = 1 << 4,
1.左移運算 1 << 4
將一個運算對象的各二進制位全部左移若干位(左邊的二進制位丟棄,右邊補0)。
1 轉(zhuǎn)化為二進制為 :0000 0001
左移四位就為 :0001 0000
0001 0000 轉(zhuǎn)化為十進制等于16
·