2014-10-30 9 views
6

0 मेरे पास एक बटन है जिसमें चयनित राज्य और सामान्य स्थिति में दो अलग-अलग पाठ हैं, जब मैं बटन की स्थिति बदलता हूं तो प्रोग्राम का आकार बदल नहीं रहा है इसलिए पाठ ठीक से दिखाई नहीं दे रहा है, जो कि सबसे अच्छा तरीका है ऑटोलायआउट के साथ ऐसा करें?यूआईबटन गतिशील चौड़ाई autolayout

मैं एक तरह से UIButton की चौड़ाई बाधा में आउटलेट की स्थापना को जानते हैं और उसे मैन्युअल लेकिन मैं बेहतर तरीका

+1

http://stackoverflow.com/questions/4135032/ios-uibutton-resize-according-to-text-length – Alfa

+0

पहले से ही पढ़ा है लेकिन अधिक मदद नहीं कर रहा है :( – Abhishek

+0

बटन की स्थिति बदलते समय- बटन आकार बड़ा करें पर्याप्त है कि स्ट्रिंग आसानी से अंदर आ सकती है लेकिन इससे पहले कि वेरिएबल में बटन का केंद्र स्टोर करें, फिर आकार विधि के लिए फिट करें और फिर बटन पर एक ही केंद्र प्रदान करें। – Alfa

उत्तर

-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

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

संबंधित मुद्दे