开发者

How to dynamically layout UIViews for a UIViewController

开发者 https://www.devze.com 2023-01-04 11:36 出处:网络
(For example) I would like to have my background image – a UIImageView – always the same size as the UIViewControllers view.

(For example) I would like to have my background image – a UIImageView – always the same size as the UIViewControllers view.

I am unable to figure this out, but sure it is possible. I'm making some UI Elements which I would like to reuse, and which can layout dynamically depending on how big my view controller's view is. How can this be done?

Is it possible not to have to set the frame of the subviews?

Here is some code for an example, in my UIViewController:

- (void)viewDidLoad {
     [super viewDidLoad];

     UIImage *backgroundImage = [[UIImage imageNamed:@"tabBarBackground.png"] stretchableImageWithLeftCapWidth:10.0f topCapHeight:10.0f];
     UIImageView *b开发者_运维百科ackgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage];

    [self.view addSubview:backgroundImageView];
    [backgroundImageView release];
}

II know I could set the frame to match the UIViewControllers frame, but what happens if this layout later need changed? Should I implement my own setNeedDisplay method in this view controller? Or can it some how be done through, maybe, autoresizingMask or other?

I would like to be able to change the view controller view's size, and then the view controller can then rearrange everything as nessicary? Is this the correct approach?

Thanks Ross


At first, you should initiate the view to the size you want it to be based on the parent view. Then you can run the following, which would keep the subview the same size as its parent as its parent is resized:

view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
0

精彩评论

暂无评论...
验证码 换一张
取 消