Trong trình tạo giao diện, giữ Lệnh + = sẽ đổi kích thước nút để phù hợp với văn bản của nó. Tôi đã tự hỏi nếu điều này đã có thể làm lập trình trước khi nút đã được thêm vào xem.iOS: UIButton đổi kích thước theo độ dài văn bản
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button.titleLabel setFont:[UIFont fontWithName:@"Arial-BoldMT" size:12]];
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
// I need to know the width needed to accomodate NSStringVariable
[button setTitle:NSStringVariable forState:UIControlStateNormal];
// So that I can set the width property of the button before addSubview
[button setFrame:CGRectMake(10, 0, width, fixedHeight)];
[subNavigation addSubview:button];
** Lưu ý cho câu hỏi rất cũ này: ** ngày nay (2017), chỉ cần đặt ràng buộc là "lớn hơn hoặc bằng" và thực hiện tất cả. (Thông báo trả lời dưới đây bởi Tad.) – Fattie