आप इस तरह डिफ़ॉल्ट बटन का उपयोग कर सकते प्रयास करें:
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
http://stackoverflow.com/questions/4135032/ios-uibutton-resize-according-to-text-length – Alfa
पहले से ही पढ़ा है लेकिन अधिक मदद नहीं कर रहा है :( – Abhishek
बटन की स्थिति बदलते समय- बटन आकार बड़ा करें पर्याप्त है कि स्ट्रिंग आसानी से अंदर आ सकती है लेकिन इससे पहले कि वेरिएबल में बटन का केंद्र स्टोर करें, फिर आकार विधि के लिए फिट करें और फिर बटन पर एक ही केंद्र प्रदान करें। – Alfa