2014-10-30 15 views
6

Mam przycisk, który ma dwa różne teksty w wybranym stanie i stanie normalnym, Kiedy zmieniam stan przycisku programowo przycisk nie jest zmieniany, więc tekst nie pojawia się poprawnie, co jest najlepszym sposobem na zrobić to z autolayout?Szerokość dynamiczna UIButton przy użyciu autolayout

Znam jeden sposób ustawienia wylot do szerokości przymusu UIButton i zmienić go ręcznie, ale szukam lepszego sposobu

+1

http://stackoverflow.com/questions/4135032/ios-uibutton-resize-zakres-do-tekstu-długości – Alfa

+0

Już to przeczytałem, ale niewiele pomogło :( – Abhishek

+0

Zmieniając stan przycisku - ustaw rozmiar przycisku na duży wystarczająco, że ciąg może łatwo wejść w to, ale przed tym uprzejmie przechowywać środek przycisku w zmiennej, a następnie użyć metody dopasowania do rozmiaru, a następnie ponownie zapewnić to samo centrum do przycisku. – Alfa

Odpowiedz

-2

Spróbuj

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

Ogólnie nie ustawiamy ramki w autolayout – Abhishek

0

Można użyć domyślny przycisk tak:

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

Jeśli użyjesz przycisku niestandardowego, najprościej wymyślisz podklas UIButton i dodasz niestandardową UILabel do środka. Oto mój krótki kod, co mam na myśli:

@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

Podczas tworzenia ograniczenia przypisać go do zmiennych

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

na check przycisk kranu czy jest wybrana lub not.Remove ograniczeń i ponownie odpowiednich ograniczeń .

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

Wystarczy mówiąc [button invalidateIntrinsicContentSize];, powinien zrobić to, czego oczekują to zrobić.

+0

To uratowało mój dzień - dziękuję! – MrBr

Powiązane problemy