适当的自动调整掩码配置

时间:2017-02-17 15:30:33

标签: ios objective-c xcode autoresizingmask

我正在尝试以编程方式复制此内容:

enter image description here

我看到苹果处理这个问题有很多困惑。我尝试了以下内容,但它对我不起作用:

[buttonTop setAutoresizingMask:UIViewAutoresizingFlexibleHeight];

任何想法从哪里开始?

更新

我按钮的代码如下:

buttonTop = [UIButton buttonWithType:UIButtonTypeCustom];
buttonTop.frame = CGRectMake(0, 0, 100, 100);
buttonTop.center = self.view.center;
buttonTop.layer.cornerRadius = buttonTop.frame.size.width / 2;
buttonTop.clipsToBounds = YES;
buttonTop.layer.masksToBounds = YES;
[buttonTop setAutoresizingMask:UIViewAutoresizingFlexibleHeight];

if (GetUserProPic){
    [buttonTop sd_setImageWithURL:[NSURL URLWithString:GetUserProPic] forState:UIControlStateNormal];
    [buttonTop sd_setImageWithURL:[NSURL URLWithString:GetUserProPic] forState:UIControlStateSelected];
} else {
    [buttonTop setImage:[UIImage imageNamed:@"avatar_icon"] forState:UIControlStateNormal];
    [buttonTop setImage:[UIImage imageNamed:@"avatar_icon"] forState:UIControlStateSelected];
}

[self.view addSubview:buttonTop];

输出如下:

enter image description here

但是,我希望它看起来像这样:

enter image description here

1 个答案:

答案 0 :(得分:0)

当包含按钮的视图改变大小时,按钮的autoResizeMask会影响按钮的大小。它不会影响按钮内的内容。 (我认为默认设置会根据需要缩小图像,但不会展开它。)

尝试此SO答案中的代码:How do I scale a UIButton's imageView?

buttonTop.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;