请考虑以下代码。在iOS 7.1上进行测试
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *button = [[UIButton alloc] init];
[self.view addSubview:button];
[button setTitle:@"Test" forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"Default"] forState:UIControlStateNormal];
button.imageView.hidden = YES;
[button sizeToFit];
NSLog(button.imageView.hidden ? @"YES" : @"NO");
//button's imageView is no longer hidden!
}
为什么sizeToFit取消隐藏imageView?
答案 0 :(得分:0)
请尝试通过将setImage属性更改为nil来隐藏图像 -
[button setImage:nil forState:UIControlStateNormal];
而不是设置hidden = YES而不是手动调整按钮的大小。