iOS8中旋转时如何调整工具栏大小

3
我正在尝试使我的应用程序底部的工具栏在旋转iPhone/iPod时重新调整大小。导航控制器会自动调整iOS 8中的导航栏大小。如果它已经处于横向状态,当活动启动时,它会得到正确的高度。但是在将其旋转为横向之后,大小与纵向相同(参见上图“Now”),因为活动是在横向模式下启动的,而我想让它变小(参见上图“Want”)。 工具栏是在故事板中添加的。
@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)中执行相同的操作,则可以正常工作,但是没有动画会显得很糟糕,因为存在一些延迟。

解决方案

来自gabbler

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

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

哇,原来如此简单。谢谢! - fknChaos

1

我进行了大量的研究,没有找到任何关于如何在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- 将工具栏拖到您的视图控制器上,并固定Leading、Bottom和Height:

enter image description here

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

enter image description here

3- 完成了。

Portrait

enter image description here


0
我不确定是否可以创建一个利用自适应的Storyboard工具栏(因为没有工具栏顶部约束的容器)。
你可以设置高度约束并在代码中进行调整,然而导航控制器的工具栏已经为您调整其高度(基于方向)。
如果您不介意在代码中设置工具栏项(使用视图控制器的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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接