Peter Warbo
Peter Warbo

Reputation: 11700

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 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.

Upvotes: 0

Views: 1230

Answers (1)

hellozimi
hellozimi

Reputation: 1878

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

Upvotes: 2

Related Questions