0
votes

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?

1
where you have set the frame for topBarview., in viewDidLoad or viewWillAppear?? - Aneesh
I have set it in viedDidLoad - Avinash Sharma
Can you try that inside viewDidAppear() method. If still the problem persist let me know - Aneesh

1 Answers

0
votes

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 :)