繁体   English   中英

在iOS8中调整旋转工具栏的大小

[英]Resizing a toolbar on rotate in iOS8

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

旋转到横向后,与纵向相同的尺寸
现在

较小,因为活动是在景观中开始的
想

工具栏已添加到故事板中。

@IBOutlet var toolbar: UIToolbar!

我试图在willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval)更改工具栏的rect 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)执行相同操作,它确实有效但没有动画它看起来很糟糕,因为有一些延迟。

来自gabbler

  1. 我为所有iphone选择了w Any h Compact (我不希望它在ipads上更小)
  2. 选中我的工具栏
  3. 在约束上将高度设置为32px,将Update框架更新 为New Constraint的项目

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

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

首先尝试使用相同的方法,但使用“ ViewWillTransitionToSize ”函数,因为不推荐使用接口旋转方法。

iOS 8轮换方法弃用 - 向后兼容性

-(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方法上实现对rect的更改。

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

1-将工具栏拖动到视图控制器并固定前导,底部和高度:

在此输入图像描述

2-控制从工具栏拖动到容器视图和引脚“等宽”

在此输入图像描述

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM