Question

I want to set the frame of a subview manually. So I just create a CGRect with CGRectMake and use the new CGRect for the frame of the subview. The problem is that the subview don't show up.
When I just use the view.bounds property of the parent view and assign this as frame to the subview then everything shows up.
I also ensured that the frame is in the displayed area.

frame = CGRectMake(0, 0, 100, 100);
[self.view addSubview:[[UIView alloc] initWithFrame:frame]];
Was it helpful?

Solution

Good way to set subview's frame is

   subview.frame = CGRectMake(0, 0, 
        CGRectGetWidth(self.view.bounds), 
        CGRectGetHeight(self.view.bounds));

Refer to link

All the values less than CGRectGetWidth(self.view.bounds) & CGRectGetHeight(self.view.bounds) should work fine

OTHER TIPS

The code in your question will create a view to which you have no reference. This means it has no background colour, contents or other means to see it, and you have no means to set it. You've added the view, but it is invisible.

Create and configure the view first, assigning it to a local variable, before adding it as a subview. As part of this configuration, give the view a background colour or some subviews.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top