ios ipad:在横向模式下将TabBar旋转到左侧,并进行变换和约束更新

我正在创build一个ios的iPad应用程序,它有一个UITabBar控件位于底部的3个MenuItems。 通常,当我旋转设备时,tabbar将保持在横向的底部。 我希望TabBar在风景模式下左侧旋转,并成为垂直表示。 如果可能的话,我不想创build一个带有button的UIView,因为我想要同一个对象来pipe理这两个对象,而且我喜欢普通TabBar的便利。

我的计划是检测设备的位置,并使用UITransform在横向上旋转TabBar 90度。 现在让我们忽略里面的图标和文本旋转,只关注整个TabBar本身。

旋转工作,除了它离开垂直TabBar在屏幕的底部中心看起来很不恰当的地方。 接下来的任务是将其限制在左侧墙上。 通常我使用3个约束来将TabBar保持在左侧/下侧/右侧。 我很难find正确的约束来正确保持它的左侧。

一个主要的问题是,一旦完成转换,高度和宽度属性在标签栏上反转(或者也转换)。 如此之高使其在屏幕上更宽,而宽度则更高。 除了顶部/底部+高度组合导致约束冲突之外,使用顶部/底部约束以及49的高度是看起来需要的。

我附上我当前的合理工作的约束逻辑,但我不明白为什么我需要硬编码这些值如图所示。 这是获得左侧视觉输出的唯一方法。 我想知道是否有一个更干净的方法来做到这一点,也许没有硬编码值的常量。

我也不确定我将如何最终旋转文本+图标作为下一步。

附件也是目前的情况。 清楚地说明是绿色的,但最终是透明的,所以景观的顶部的截止点不会成为问题。

- (void) adjustViewsForOrientation:(UIInterfaceOrientation) orientation { switch (orientation) { case UIInterfaceOrientationPortrait: case UIInterfaceOrientationPortraitUpsideDown: { //load the portrait view // revert to normal transform _tabBar.transform = CGAffineTransformRotate(CGAffineTransformIdentity, M_PI*2); [self.view removeConstraint:_tabbarConstraintT]; [self.view removeConstraint:_tabbarConstraintR]; [self.view removeConstraint:_tabbarConstraintB]; [self.view removeConstraint:_tabbarConstraintL]; // right space _tabbarConstraintR = [NSLayoutConstraint constraintWithItem:_tabBar attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0]; // bottom space _tabbarConstraintB = [NSLayoutConstraint constraintWithItem:_tabBar attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]; // left space _tabbarConstraintL = [NSLayoutConstraint constraintWithItem:_tabBar attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0]; //[self.view addConstraint:_tabbarConstraintT]; [self.view addConstraint:_tabbarConstraintR]; [self.view addConstraint:_tabbarConstraintB]; [self.view addConstraint:_tabbarConstraintL]; } break; case UIInterfaceOrientationLandscapeLeft: case UIInterfaceOrientationLandscapeRight: { //load the landscape view _tabBar.transform = CGAffineTransformRotate(CGAffineTransformIdentity, M_PI/2); [self.view removeConstraint:_tabbarConstraintT]; [self.view removeConstraint:_tabbarConstraintR]; [self.view removeConstraint:_tabbarConstraintB]; [self.view removeConstraint:_tabbarConstraintL]; NSLog(@"frame: %f,%f",_tabBar.frame.size.height,_tabBar.frame.size.width); // top space _tabbarConstraintT = [NSLayoutConstraint constraintWithItem:_tabBar attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:[UIScreen mainScreen].bounds.size.height/2+49]; // left space _tabbarConstraintL = [NSLayoutConstraint constraintWithItem:_tabBar attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:-360.0]; // effective width _tabbarConstraintR = [NSLayoutConstraint constraintWithItem:_tabBar attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributeHeight multiplier:1.0f constant:49]; // effective height _tabbarConstraintB = [NSLayoutConstraint constraintWithItem:_tabBar attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributeWidth multiplier:1.0f constant:[UIScreen mainScreen].bounds.size.height]; [self.view addConstraint:_tabbarConstraintR]; [self.view addConstraint:_tabbarConstraintB]; [self.view addConstraint:_tabbarConstraintT]; [self.view addConstraint:_tabbarConstraintL]; } break; case UIInterfaceOrientationUnknown:break; } } 

在这里输入图像说明在这里输入图像说明