Objective-C – Positioning of views inside a view
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
精彩评论