【问题标题】:Resizing a toolbar on rotate in iOS8在 iOS8 中调整旋转工具栏的大小
【发布时间】:2014-10-28 12:57:13
【问题描述】:

我试图让我的应用程序底部的工具栏在我旋转 iphone/ipod 时调整大小。导航控制器在 iOS8 中自动调整导航栏的大小。如果在活动开始时它已经处于横向状态,它确实会获得正确的高度。

旋转为横向后与纵向大小相同

较小,因为活动是在横向开始的

工具栏已添加到情节提要中。

@IBOutlet var toolbar: UIToolbar!

我试图改变willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval)中工具栏的矩形

像这样:

override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
    var customToolbarFrame: CGRect!
    if toInterfaceOrientation == UIInterfaceOrientation.Portrait {
        customToolbarFrame = CGRectMake(0,self.view.bounds.size.height - 44, self.toolbar.frame.size.width, 44)
    }
    else {
        customToolbarFrame = CGRectMake(0,self.view.bounds.size.height - 32, self.toolbar.frame.size.width, 32)
    }
    UIView.animateWithDuration(duration, animations: {
        self.toolbar.frame = customToolbarFrame
    })
}

我也尝试过不使用动画,但没有运气。

但是,如果我在 didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) 中执行相同的操作,它确实可以工作,但如果没有动画,它看起来很糟糕,因为有一些延迟。

解决方案

来自胡言乱语

  1. 我为所有 iphone 选择了 w Any h Compact(我不希望它在 ipad 上变小)
  2. 选择了我的工具栏
  3. 在约束上将高度设置为 32px,并更新帧新约束项

【问题讨论】:

    标签: ios iphone swift ios8


    【解决方案1】:

    如果您使用自动布局,您可以在纵向和横向设置不同的高度限制,例如在 wCompact|hCompact 模式下为工具栏添加 32 高度限制,您将能够在横向模式下看到 32 高度工具栏,这里是Toolbar test example

    【讨论】:

    • 哇。就这么简单。谢谢!
    【解决方案2】:

    我做了很多研究,但没有找到任何解释如何在 IOS8 中更改/修复导航控制器工具栏高度的文档,但我使用 Autolayout 构建了一个解决方法。

    首先尝试了与您相同的方法,但使用“ViewWillTransitionToSize”函数,因为界面旋转的方法已被弃用。

    iOS 8 Rotation Methods Deprecation - Backwards Compatibility

    -(void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    
        [super viewWillTransitionToSize: size withTransitionCoordinator: coordinator];
    
        [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
    
            UIDeviceOrientation deviceOrientation   = [[UIDevice currentDevice] orientation];
    
            if (UIDeviceOrientationIsLandscape(deviceOrientation)) {
                NSLog(@"Will change to Landscape");
    
            }
            else {
                NSLog(@"Will change to Portrait");
            }
    
    
        }
        completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
            NSLog(@"finish Transition");
        }];
    
    
    }
    

    第二我尝试构建一个继承自 UIToolbar 的自定义类,并在 initWithFrame 方法上实现对矩形的更改。

    如果您想使用自动布局实施解决方法来解决此问题,请按照以下步骤操作:

    1- 将工具栏拖到您的视图控制器和引脚前导、底部和高度:

    2- 控制从工具栏拖动到容器视图并固定“等宽”

    3- 完成:

    【讨论】:

      【解决方案3】:

      我不确定是否可以创建一个利用适应的情节提要工具栏(因为没有用于工具栏顶部约束的容器)。

      您可以设置一个高度约束并在代码中调整它,但是导航控制器的工具栏已经为您调整了它的高度(基于方向)。

      如果您不介意在代码中设置工具栏项(使用视图控制器的 toolbarItems 属性),您将免费获得自适应大小。

      self.toolbarItems = @[...];
      

      这是我如何设置工具栏的示例:

      /**
       Setup a segmented control of bible versions, and add it to the toolbar
       */
      - (void)setupBibleVersionToolbarItems
      {
          self.versionSegmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"ESV",
                                                                                     @"KJV",
                                                                                     @"NASB",
                                                                                     @"NIV",
                                                                                     @"NKJV",
                                                                                     @"NLT"]];
          self.versionSegmentedControl.opaque = NO;
          [self.versionSegmentedControl addTarget:self
                                           action:@selector(versionChanged:)
                                 forControlEvents:UIControlEventValueChanged];
      
          UIBarButtonItem *bibleVersionItem = [[UIBarButtonItem alloc]
                                               initWithCustomView:(UIView *)self.versionSegmentedControl];
          bibleVersionItem.width = 300.0f;
          UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc]
                                            initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                            target:nil
                                            action:nil];
      
          self.toolbarItems = @[flexibleSpace, bibleVersionItem, flexibleSpace];
      }
      

      您可以根据需要通过设置其toolbarHidden 属性来显示或隐藏导航控制器的工具栏。

      - (void)viewWillAppear:(BOOL)animated
      {
          [super viewWillAppear:animated];
          self.navigationController.toolbarHidden = NO;
      }
      
      - (void)viewWillDisappear:(BOOL)animated
      {
          [super viewWillDisappear:animated];
          self.navigationController.toolbarHidden = YES;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-23
        • 2015-09-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多