简体   繁体   English

更改UIView内的UIButton框架

[英]Change UIButton frame inside UIView

I want display different UIButton / UILabel / UISlider sizes on an iPhone 4/4s screen. 我想在iPhone 4 / 4s屏幕上显示不同的UIButton / UILabel / UISlider尺寸。 In the parent ViewController viewDidAppear: method I was able to set new button frames by calling my viewFormatting function. 在父ViewController viewDidAppear:方法中,我能够通过调用viewFormatting函数来设置新的按钮框架。

- (void) viewFormatting
{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        CGSize result = [[UIScreen mainScreen] bounds].size;

        //iPhone 4
        if (result.height == 480)
        {
            self.myButton.frame = CGRectMake(x, y, width, height);
            self.myLabel.frame = CGRectMake(x, y, width, height);
            //etc.
        }
    }
}

But when I try calling the same method viewFormatting inside initWithFrame: on separate UIView class the button/label frames do not resize. 但是,当我尝试在单独的UIView类上的initWithFrame:调用相同的viewFormatting方法时,按钮/标签框架不会调整大小。 I also tried calling the function inside layoutSubviews . 我还尝试在layoutSubviews内部调用该函数。 I put a NSLog inside viewFormatting so I know it is getting called correctly, but the button/label frames do not change. 我将NSLog放在viewFormatting因此我知道它被正确调用了,但是按钮/标签框架没有改变。

Note: In both scenarios I have created the outlets in XIB files. 注意:在这两种情况下,我都在XIB文件中创建了出口。

Any help is greatly appreciated! 任何帮助是极大的赞赏!

Update: Thanks to @nburk I realized that the use AutoLayout was checked on my xib file. 更新:感谢@nburk,我意识到在我的xib文件中检查了AutoLayout的使用。 After removing that the code above works perfectly. 删除后,上面的代码可以完美地工作。

I'll leave it here for anyone who needs a sample. 我将其留给需要样品的任何人。

Add this method: -(void) viewDidLayoutSubviews { [super viewDidLayoutSubviews]; 添加此方法:-(void)viewDidLayoutSubviews {[super viewDidLayoutSubviews]; [self viewFormatting]; [self viewFormatting]; } }

viewDidLayoutSubviews get called automatically. viewDidLayoutSubviews会自动调用。

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

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