Fixed size UIView, centered in the view - ios

I want to have an UIView with width=480 and height=640, centered in the middle of my master view.
I want this to happen both in landscape orientation and portrait orientation, but now if I design it for portrait orientation, when I'm turning the device to landscape, my UIView has width=640 and height=480.
Any solutions for my UIView to keep it's sizes in both orientation types?

you can implement viewDidLayoutSubviews (in UIViewController) or layoutSubviews (in UIView) to reposition the view.
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
self.myCustomView.bounds = CGRectMake(0, 0, 480, 640); // update size
self.myCustomView.center = self.view.center; // update center
}

Related

iOS part of viewcontroller goes black when orientation changes

When I change viewcontroller orientation while it's loading on particular orientation, part of the screen goes blank at the bottom.
Steps to reproduce:
Have tableview(Not necessary I think. could be any view) in a view controller
Push a new viewcontroller which also has a tableview(Not necessary I think. could be any view) during
an action
Rotate the screen from one orientation to Another(Portrait to landscape or landscape to portrait).now you will be able to see the dark part.
Press back button to pop the current viewcontroller
Now rotate from one orientation to Another(Portrait to landscape or landscape to portrait).now you will be able to see the dark part here as well.
The bottom part of viewcontroller goes black.
I am able to reproduce 7 out of 10 times.
FYI:
My Viewcontroller has only tableview that's all and all cells has autolayout constraints.
Trying to understand why it happened and I would like to fix it.
More details on reproducing the issue:
Basically you can reproduce this issue only if you hold your device in slanting position and either push or pop a view controller and while the page is trying to load , you have to change the orientation then the issue happens.
(Pun intended.... lol )
In other words ,you have to tilt you device as if you are steering the wheel :).
This issue can only happen if you use your device like a steering wheel :)
In your viewWillTransition method call layoutIfNeeded()
Try to reset the layout of TableView after rotation by using this method,
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
tableView.layoutIfNeeded()
}
or
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
tableView.layoutIfNeeded()
}
I also experienced the same issue.
My view controller had just one tableview and when we tilt , I mean change the orientation during a navigational push or pop this used to happen.
Try setting the frame
in viewDidLayoutSubviews
-(void)viewDidLayoutSubviews{
CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat width = CGRectGetWidth(screen);
CGFloat height = CGRectGetHeight(screen);
CGRect frame = self.tableView.frame;
// To check if there's any mismatch between the sizes of screen and tableview then set the tableview frame same as screen frame.
if (CGRectGetHeight(frame) != height && CGRectGetWidth(frame) != width) {
self.tableView.frame = CGRectMake(0, 0, width, height);
}
}
or try setting the frame in viewWillTransitionToSize
-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat width = CGRectGetWidth(screen);
CGFloat height = CGRectGetHeight(screen);
CGRect frame = self.tableView.frame;
if (CGRectGetHeight(frame) != height && CGRectGetWidth(frame) != width) {
self.tableView.frame = CGRectMake(0, 0, width, height);
}
}
FYI:
if setting the frame self.tableView.frame = CGRectMake(0, 0, width, height); doesn't work then you could try [self.tableView layoutIfNeeded]
Set frame again in
viewDidLayoutSubviews
In one view controller(Parent the one that pushes new view controller) setting the frame at viewDidLayoutSubviews solves the issue in another view controller(The child view controller , one that's getting pushed) setting the frame at viewWillTransition fixes the problem :) But still I don't know why there's no universal solution to this problem.

UiView frame not working

I have a UIView,
UIView *topBarView
I am deciding the size i.e. the width and height of topBarView as
CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height*.05);
Now I am setting the frame of topBarView as
topBarView.frame = (CGRect){0, 0,topBarSize};
Im my view controller I have set this condition for rotation
- (BOOL)shouldAutorotate
{
return YES;
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
When my view controller opens in portrait mode it is all good and as expected the topBarView is placed at the top
But when my view opens in Landscape Left mode the topBarView instead of being on top in Landscape is on the left of the screen i.e same frame as it was in case of portrait
How can I fix this?
As you have set frame your view's origin is (0,0) so it will definitely set in top - left corner in any orientation.
If you want to manage width and height according to orientation then you should use autolayout.
You should set top,leading,trailing and fixed height constraint to that view. so, it will will manage height,width and position with orientation.
Update:(as asked in comment)
Write below code in viewdidload and comment yours
UIView *topBarView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height * 0.5)];
topBarView.backgroundColor = [UIColor orangeColor];
[self.view addSubview: topBarView];
Hope this will help :)

ScrollView doesn't work in landscape orientation

I am developing in Objective-C. I am using the following code for scrollView, and it works fine in Portrait orientation.
- (void)viewDidLayoutSubviews
{
UIScrollView *scrollView = self.scrollview;
[scrollView setContentSize:CGSizeMake(320.0,3100.0)];
}
But the view does not slide down when I change to landscape orientation.
Am i missing something?
[scrollView setContentSize:CGSizeMake(320.0,3100.0)];
In this, 320 used for Width.. When you are trying for landscape orientation. Then please increase size for width..
Please check it with more than 320 (Minimum Screeen Width).
3.5 inch - 480 & 4 inch - 568
[scrollView setContentSize:CGSizeMake(SCREEN_WIDTH + YOUR_EXTRA_SIZE,3100.0)];
Hopefully, it will help you.
Thanks.
- (void)viewWillAppear:(BOOL)animated
{
UIScrollView *scrollView = self.scrollview;
[scrollView setContentSize:CGSizeMake(320.0,3100.0)];
}
Scrollview always loaded in portrait mode in viewDidLoad/loadView method, so what ever adjustment you like to made you have to do those changes in viewWillAppear/viewDidAppear methods.
When Device get rotate didRotateFromInterfaceOrientation method get called, so I would suggest to change the ScrollView Contant size this way
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
self.scrollView.contentSize = self.contentView.bounds.size;
}
Note : Make sure your have UIScrollView and UIView in your layout
UIScrollView is scrollView and
UIView is contentView

iOS - View rotate and resize

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

How can I adjust a UIView for landscape orientation?

I have a UITableViewController with a custom UIView as a subview. When I rotate the iPhone from portrait to landscape orientation the UIView subview rotates correctly but does not resize correctly. How can I resize the custom UIView when the device is rotated?
Set the UIView's autoresizingMask like so:
myView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
This should mean that the view resizes itself automatically on rotation.

Resources