UIButton图像视图在调整大小时变为unhidden

时间:2014-06-09 17:20:12

标签: ios objective-c cocoa-touch

请考虑以下代码。在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?

1 个答案:

答案 0 :(得分:0)

请尝试通过将setImage属性更改为nil来隐藏图像 -

[button setImage:nil forState:UIControlStateNormal];

而不是设置hidden = YES而不是手动调整按钮的大小。