To set a UIView to take up the entire screen, is this correct?
self.mapView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
Or should I be using the self.view.bounds...?
You can use either of frame and bounds for view as frame and bounds are same for it. But when you work with subviews then use bounds because frame and bounds need not be the same. For subview they are same only when your subview's size is same as its superview.
Related
I should know this but don't and can't find it explained anywhere.
I am moving a UIView in the coordinate space of the window and would like its subview (a tableView) added in code to move as well. I have not added any explicit constraints linking the subview to its superview thinking they would move in tandem. The tableview is not moving, however, as far as I can tell, when I move the superview.
Is it normal behavior for a subview created in code to be unaffected by changing the coordinates of its superview? If so, do you have to add constraints in code, should you manually move the subviews at the same time you are moving the superview or how can you get the subview to move in tandem? Here is code:
//Create view and subview (tableView):
myView= [UIView new];
CGFloat width = self.view.frame.size.width;
CGFloat height=self.tableView.frame.size.height;
//Place offscreen
[myView setFrame:CGRectMake(-width, 0, width, height)];
[self.view addSubview:myView];
aTableView = [UITableView new];
//Initially set frame to superview
aTableView.frame = myView.frame;
[myView addSubview:aTableView];
//Move superview on screen
myRect = CGRectMake(0,0,width,height)];
myView.frame = myRect;
myView moves but Tableview does not seem to move from this alone. How can I move it?
I'm assuming you say "myView moves but Tableview does not seem to move" because you don't see Tableview on-screen? If so, it looks like it's due to the way you set the frame(s).
//Create view and subview (tableView):
myView= [UIView new];
CGFloat width = self.view.frame.size.width;
CGFloat height=self.tableView.frame.size.height;
//Place offscreen
[myView setFrame:CGRectMake(-width, 0, width, height)];
[self.view addSubview:myView];
OK - myView is now off-screen to the left. Let's assume a width of 320 and a height of 480, so your myView's frame is (for example):
`-320, 0, 320, 480`
then
aTableView = [UITableView new];
//Initially set frame to superview
aTableView.frame = myView.frame;
[myView addSubview:aTableView];
Whoops, you set aTableView.frame = myView.frame; That means your table's frame is:
`-320, 0, 320, 480`
but that is relative to myView's frame. So your table view is located 320-ptd to the left of myView, which comes out to 640-pts to the left of the left edge of the screen.
//Move superview on screen
myRect = CGRectMake(0,0,width,height)];
myView.frame = myRect;
Now you've moved myView's left to 0, so it's visible, but aTableView is still 320-pts to the left of myView, so it's still off-screen.
Changing that one line to:
aTableView.frame = myView.bounds
should take care of it.
When I set UIScrollView clipsToBounds property to NO, it allowed me to display the content views horizontally outside its bound. That is what I wanted to achieve. But it also allowed to scroll pass top and bottom bounds which I do not want.
Can UIScrollView clipsToBounds be applied vertically only?
(btw: I want to effect just as in the picture so I do not want to expand the content view horizontally.)
One solution would be to embed scroll view inside another view, which would have clipToBounds = NO, while scroll view would have clipToBounds = YES and width equal to content beign displayed.
As I am not good with images, I try example by code:
wrapperView.frame = CGRectMake(0, 0, 300, 300);
wrapperView.clipToBounds = NO;
scrollView.frame = CGRectMake(0, 0, 400, 300);
scrollView.clipToBounds = YES
[wrapperView addSubview:scrollView];
contentView.frame= CGRectMake(0, 0, 400, 1234);
[scrollView addSubview:contentView];
I have placed UIView in UIScrollView in Storyboard. The UIView is pinned to the all the sides of the UIScrollView. I am trying to set the size of UIView programatically because it is needed for scrolling to work properly. How can I programmatically set the width and height of the UIView?
Thanks for any help as I am trying to solve the problem already for a week...
You'll need to change the frame of your view as below:
yourView.frame = CGRectMake(x,y,calculatedWidth,calculatedHeight);
Plus, you'll also need to check for the scroll view's frame so that it enlarges as per your view's width and height.
You need to set new frame to change width, height, x or y for example:
view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, newWidth, newHeight);
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]];
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
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.
Here is myView
When
self.myView.transform = CGAffineTransformMakeRotation(M_PI*0/180);
[self.myView setFrame:CGRectMake:(self.myView.frame.orgin.x,self.myView.frame.orgin.y,300,self.myView.frame.size.height)];
it will be
now when i rotate the view
And do this again
self.myView.transform = CGAffineTransformMakeRotation(M_PI*0/180);
[self.myView setFrame:CGRectMake:(self.myView.frame.orgin.x,self.myView.frame.orgin.y,300,self.myView.frame.size.height)];
What happened here?
seen like the width is stretched.
Its anyway to fix it or have other way to rotate and resize?
You should use the bounds and center properties to resize and place your view, frame should not be used if the transform property is not the identity transform
(see warning at the UIView documentation for frame)
frame.size is not equal to bounds.size when your view is rotated, if you want your view to have a width of 300, you should set the bounds