开发者

Objective-C – Positioning of views inside a view

开发者 https://www.devze.com 2023-04-05 14:36 出处:网络
Is there a way to add some subviews to a view and then be able to position that开发者_如何学Python view so all the subviews will \"follow\". Acting as a container so to speak for the subviews so I onl

Is there a way to add some subviews to a view and then be able to position that开发者_如何学Python view so all the subviews will "follow". Acting as a container so to speak for the subviews so I only need to reposition the view and not having to reposition all the subviews in that view.

I'm looking for a solution to do this programtically, not in IB.


If you use a UIView as a parent view all subviews will follow.

UIView *parentView = [[UIView alloc] initWithFrame:self.view.bounds]
[self.view addSubview:parentView]; // Assuming self is a ViewController
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn1.frame = CGRectMake(10, 10, 120, 44);
[parentView addSubview:btn1];

// Sets a new position with the same view size
parentView.frame = CGRectMake(50, 100, self.view.bounds.size.width, self.view.bounds.size.height);

Now when you set the new frame of the parentView. The btn1 will have it's position relative to the parentView.

I Hope I understood your question correctly.

EDIT: Added comments

0

精彩评论

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