简体   繁体   中英

Change UIButton frame inside UIView

I want display different UIButton / UILabel / UISlider sizes on an iPhone 4/4s screen. In the parent ViewController viewDidAppear: method I was able to set new button frames by calling my viewFormatting function.

- (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. I also tried calling the function inside layoutSubviews . I put a NSLog inside viewFormatting so I know it is getting called correctly, but the button/label frames do not change.

Note: In both scenarios I have created the outlets in XIB files.

Any help is greatly appreciated!

Update: Thanks to @nburk I realized that the use AutoLayout was checked on my xib file. 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]; [self viewFormatting]; }

viewDidLayoutSubviews get called automatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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