【问题标题】:UiView frame not workingUIView 框架不工作
【发布时间】:2016-06-02 08:07:23
【问题描述】:

我有一个 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 而不是在横向模式中位于顶部,而是在屏幕的左侧,即与纵向模式相同的帧

我该如何解决这个问题?

【问题讨论】:

  • 您在 viewDidLoad 或 viewWillAppear 中设置 topBarview 框架的位置??
  • 我已经在 viedDidLoad 中设置了
  • 你能在 viewDidAppear() 方法里面试试吗。如果问题仍然存在,请告诉我

标签: ios objective-c uiview cgrect


【解决方案1】:

由于您已经设置了视图的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];

希望这会有所帮助:)

【讨论】:

  • 嗨,我只是想让我的 topBarView 在我的视图控制器加载时保持在顶部
猜你喜欢
  • 2017-10-16
  • 2013-08-18
  • 2015-01-07
  • 2016-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多