更改的UIButton的大小,而使用自动布局? [英] Changing size of UIButton while using Autolayout?

查看:404
本文介绍了更改的UIButton的大小,而使用自动布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是pretty新到iOS。我的工作是要经常使用的整个应用程序有一定UIView类的项目。这个类很简单,做一些透明度的东西用背景色的图像。这工作得很好至今。该类有时存在作为一个孤独的UIView,有时作为一个按钮子视图。

当我这样的UIView添加到一个UIButton作为一个子视图,UIView的的全部内容以全尺寸显示,但按钮的点击区域保留在厦门国际银行所定义的大小,除非使用自动布局处于关闭状态(即使我尝试手动设置按钮的框架。)

我想提出一个UIButton在厦门国际银行作为占位符,然后再确定其大小/点击区域的基础上叠加图像,这与初始化的UIView的大小。

如果这是可能的,还是我misinter preting厦门国际银行的文件/自动版式的使用情况如何?

viewDidLoad中下我有...

   - (无效)viewDidLoad中{    [theButton addSubview:_theView];
    的CGRect buttonFrame = theButton.frame;
    buttonFrame.size = CGSizeMake(_theView.getSize.width,_theView.getSize.height);
    [theButton SETFRAME:buttonFrame];
}

然而,帧保持相同的大小,当我打印按钮信息之前和之后我尝试调用SETFRAME。

(注意,'_theView.getSize被我加的)


解决方案

  1. 在自动布局,视图不必在 viewDidLoad中帧。尝试在您code viewWillAppear中: viewDidLayoutSubviews

  2. 在自动布局,你没有设置帧。您可以编辑约束来代替。设置框将工作,直到下一个布局传递,当你的布局将恢复到您的约束描述。

  3. 要大小按钮,以适应子视图,你可以尝试这样的事情(以视觉形式语言): | - [theView] - | ,但是这将取决于什么约束是从厦门国际银行的地方。

I'm pretty new to ios. I'm working on a project which is going to frequently use a certain UIView class throughout the application. This class is simply an image that does some transparency stuff with the background color. This works fine so far. This class sometimes exists as a lone UIView, and sometimes as a button subview.

When I add this UIView to a UIButton as a subview, the full contents of the UIView are displayed at full size, but the clickable area of the button remains the size defined in the xib unless Use Autolayout is turned off (even if I manually try to set the button's frame.)

I would like to put a UIButton on the xib as a placeholder, and then later define its size/clickable area based on the size of the overlay image which the UIView that was initialized with.

Should this be possible, or am I misinterpreting the usage of xib files/autolayout?

under ViewDidLoad I have...

- (void)viewDidLoad{

    [theButton addSubview:_theView];
    CGRect buttonFrame = theButton.frame;
    buttonFrame.size = CGSizeMake(_theView.getSize.width,_theView.getSize.height);
    [theButton setFrame:buttonFrame];
}

However, the frame stays the same size when I print the button info before and after I try calling setFrame.

(note that '_theView.getSize' was added by me)

解决方案

  1. Under Autolayout, views don't have frames at viewDidLoad. Try your code in viewWillAppear: or viewDidLayoutSubviews.
  2. Under Autolayout, you don't set frames. You edit constraints instead. Setting a frame will work until the next layout pass, when your layout will revert to that described by your constraints.
  3. To size a button to fit a subview, you can try something like this (in visual format language): |-[theView]-| but it would depend what constraints are in place from your xib.

这篇关于更改的UIButton的大小,而使用自动布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆