2014-10-30 61 views
6

我有一個按鈕,它在選中狀態和正常狀態下有兩個不同的文本,當我以編程方式更改按鈕的狀態時,按鈕沒有被調整大小,所以文本顯示不正確,這是用autolayout做到這一點?使用自動佈局的UIButton動態寬度

我知道的一種方式設置出口的UIButton的寬度約束和手動更改,但我正在尋找更好的方式

+1

http://stackoverflow.com/questions/4135032/ios-uibutton-resize-according-to-text-length – Alfa 2014-10-30 11:36:06

+0

已經讀過,但沒有太大的幫助:( – Abhishek 2014-10-30 11:46:15

+0

雖然改變按鈕的狀態 - 使按鈕尺寸大足夠的字符串可以很容易地進入它,但之前,善意地存儲在變量中的按鈕的中心,然後使用適合大小的方法,然後再次提供相同的中心按鈕 – Alfa 2014-10-30 11:50:20

回答

-2

試試這個

CGSize maxSize = CGSizeMake(btn.bounds.size.width, CGFLOAT_MAX); 
    CGSize textSize1 = [btn.titleLabel.text sizeWithFont:btn.titleLabel.font constrainedToSize:maxSize]; 

btn.frame=CGSizeMake(10,10,textSize1.width,30); 
+2

通常我們不在自動佈局中設置框架 – Abhishek 2014-10-31 05:37:21

0

你可以使用默認的按鈕是這樣的:

UIButton* button = [UIButton buttonWithType:UIButtonTypeSystem]; 
[button setTranslatesAutoresizingMaskIntoConstraints:NO]; 
[button setTitle:@"Normal" forState:UIControlStateNormal]; 
[button addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside]; 
[button setTitle:@"Selected" forState:UIControlStateSelected]; 
[self.view addSubview:button]; 
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[button]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(button)]]; 
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[button]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(button)]]; 

如果您使用自定義按鈕,那麼我能想到的最簡單的方法是子類UIButton並在其中添加您的自定義UILabel。這裏是我的短我的意思代碼:

@interface CustomButton : UIButton 
{ 
    NSString* _normalTitle; 
    NSString* _selectedTitle; 
} 
@property UILabel* customLabel; 

@end 

@implementation CustomButton 

@synthesize customLabel=_customLabel; 

- (instancetype)init; 
{ 
    self = [super init]; 
    if (self) { 
    [self setBackgroundColor:[UIColor greenColor]]; 

    _customLabel = [UILabel new]; 
    [_customLabel setTextColor:[UIColor whiteColor]]; 
    [_customLabel setTranslatesAutoresizingMaskIntoConstraints:NO]; 
    [self addSubview:_customLabel]; 

    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[_customLabel]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_customLabel)]]; 
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_customLabel]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_customLabel)]]; 
} 
return self; 
} 
- (void)setTitle:(NSString *)title forState:(UIControlState)state; 
{ 
    if (state == UIControlStateNormal) { 
     _normalTitle = title; 
    } else if (state == UIControlStateSelected) { 
     _selectedTitle = title; 
    } 
    [self setSelected:[self isSelected]]; 
} 
- (void)setSelected:(BOOL)selected; 
{ 
    [super setSelected:selected]; 
    if (selected) { 
     [_customLabel setText:_selectedTitle]; 
    } else { 
     [_customLabel setText:_normalTitle]; 
    } 
} 

@end 
0

當你創建的約束將其分配給它是否被選中的變量

@property (nonatomic, strong)NSLayoutConstraint *viewConstraintTop; 
self.viewConstraintTop = [Your Constraint];//Initial state 

在按鈕自來水支票或not.Remove的約束和重新申請適當的約束。

[self.viewConstraintTop autoRemove]; 
self.viewConstraintTop = [Your Constraint]; 
3

只要告訴[button invalidateIntrinsicContentSize];,它應該做你期待它做的事情。

+0

這節省了我的一天 - 謝謝! – MrBr 2015-07-15 08:48:07