简体   繁体   English

使用CGRectMake重绘UIButton不会更新UIButton位置吗?

[英]Redrawing UIButton using CGRectMake doesn't update UIButton position?

I'm trying to update the position and size of several UIButtons in my app by using: 我正在尝试使用以下方法更新我的应用程序中几个UIButton的位置和大小:

-(void)viewDidAppear:(BOOL)animated {
    _UIButton1.frame = CGRectMake(-7, 35, 212, 77);
    _UIButton2.frame = CGRectMake(139, 149, 290, 77);
    // And so on for several more buttons
}

However, when I use this code, these buttons just appear at wherever the location is set on the Storyboard, not where I'm setting it programmatically. 但是,当我使用此代码时,这些按钮仅出现在情节提要上设置的位置的任何地方,而不是我以编程方式设置的位置。 How would I make these buttons appear at a location this is different than the one I have set in my Storyboard? 如何使这些按钮显示在与我在情节提要中设置的按钮不同的位置?

Open your storyboard file and make sure the right pane is showing the Utilities menu, then uncheck Use Auto Layout . 打开情节提要文件,并确保右窗格显示“实用程序”菜单,然后取消选中“ Use Auto Layout

When Auto Layout is enabled, you have to set constraints on your views and create outlets to those constraints to move things around with code. 启用“自动版式”后,您必须在视图上设置约束并为这些约束创建出口以使用代码移动内容。 If you want to work directly with frames, Auto Layout needs to be turned off. 如果要直接使用框架,则需要关闭自动版面。 It has to be one or the other. 它必须是另一个。

Auto Layout can be great, but it's hard to make a recommendation without knowing more about what you are trying to achieve. “自动布局”可能很棒,但是如果不了解您要实现的目标,就很难提出建议。

Please update frame in -(void) viewWillAppear method as in viewDidAppear your view is practically set and is ready to displayed. 请像在viewDidAppear那样更新-(void) viewWillAppear方法中的框架,实际上已经设置了视图并可以显示它了。 after updating your button's frame please use following line of code 更新按钮的框架后,请使用以下代码行

[self.view laoutIfNeeded];

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM