Reputation: 8556
I' m encountered with a problem because of my lack of knowledge of iOS platform I think.
I have a view controller User Profile
. The main view has next structure: ScrollView
which contains one subview and this UIView
has 7 UIViews
with UILabels
, UItextViews
, UIImageViews
, etc. each of them displays specific info about user. The problem - data for this views is fetched from the server and it can be different. E.g I have a subview Education, it contains next info: name of the institute, degree, years of learning, etc. But one user can have several educations. So the size of this view can be dynamic. But i can't simply change the view size with view.frame = CGRectMake
because under it I have a view job experience and etc. Or for example professional skills view: it can be one skill and it can be one hundred skills for one user -so I need to change the size of this view but under it I have another view so i need to move it and so on and so forth.
So the question what is the correct way of dealing with this situation? I understood that I can't just change view frames with view.frame= CGRectMake()
- because it is too much work and it is a stupid approach I think. I know there must be some more straightforward way for this very common problem, may be autolayout
or something else? Anyway I hope for some help and advice of how can I make views with dynamic content.
Upvotes: 1
Views: 1287
Reputation: 9012
As you've stated, manually updating the frames is a lot of work and is the old way of doing things. The new better way as you've also said is to use auto layout. Describing how to use auto layout to position your views would require more specific information on how you want it to look but there are some awesome tutorials out there on the web! Here's one of many:
Ray Wenderlich autolayout in iOS 7 part 1
Upvotes: 0
Reputation: 6432
I really don't know why you said "I can't just change view frames". Of course you can!
The approach I always take to this scenario (where your view's height is variable) is to declare a yOffset
property and based on this I place my content at the right y
position.
@property (nonatomic, assign) int yOffset;
Then inside the init
method I initialize this property to either 0 or some predefined initial margin.
_yOffset = TOP_MARGIN;
Then consider the following scenario: a user profile with n
number of skills. This is how you would use the yOffset
property.
for (int i=0; i<n; ++i)
{
// place the skillView at the right 'y' position
SkillView *skillView = [[SkillView alloc] initWithFrame:CGRectMake(0,self.yOffset,300,50)];
// configure the skillView here
[self.view addSubview:skillView];
// Don't forget it has to be "+=" because you have to keep track of the height!!!
self.yOffset += skillView.frame.size.height + MARGIN_BETWEEN_SKILLS;
}
And then imagine the user profile has c
number of Education entries; same thing:
for (int i=0; i<c; ++i)
{
// place the educationView at the right 'y' position
EducationView *educationView = [[EducationView alloc] initWithFrame:CGRectMake(0,self.yOffset,300,50)];
// configure the educationView here
[self.view educationView];
// Don't forget it has to be "+=" because you have to keep track of the height!!!
self.yOffset += educationView.frame.size.height + MARGIN_BETWEEN_EDUCATIONS;
}
Finally, when all your subviews have been added you have to change the frame of the containing view. (Because when you created it you couldn't know upfront how tall it was going to be)
CGRect viewFrame = self.view.frame;
viewFrame.size.height = self.yOffset + BOTTOM_MARGIN;
self.view.frame = viewFrame;
And that's it.
Hope this helps!
Upvotes: 3