UiView框架无法正常工作

时间:2016-06-02 08:07:24

标签: ios objective-c uiview cgrect

我有一个UIView,

UIView *topBarView

我决定将topBarView的大小即宽度和高度设为

CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height*.05);

现在我将topBarView的框架设置为

topBarView.frame = (CGRect){0, 0,topBarSize};

我的视图控制器我已将此条件设置为旋转

- (BOOL)shouldAutorotate
{
  return YES;
}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
  return UIInterfaceOrientationMaskPortrait;
}

当我的视图控制器以纵向模式打开时,一切都很好,并且正如预期的那样topBarView位于顶部

但是当我的视图在横向左侧模式中打开时,topBarView而不是位于Landscape的顶部位于屏幕的左侧,即与纵向相同的框架

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

由于您已将视图设置为origin is (0,0),因此它肯定会设置在任意方向的左上角。

如果您想根据方向管理宽度和高度,则应使用autolayout

您应该将top,leading,trailing and fixed height约束设置为该视图。因此,它将根据方向管理高度,宽度和位置。

更新:(如评论中所述)

在viewdidload中编写以下代码并发表评论

   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];

希望这会有所帮助:)

相关问题