如何在UIToolbar上隐藏小尺寸类的UIBarbuttonItem?

时间:2016-02-13 10:18:28

标签: ios uibarbuttonitem uitoolbar size-classes

很遗憾UIToolbar不支持UIBarbuttonItem元素的大小等级。如何在UIBarbuttonItem上隐藏UIToolbar小尺寸课程?

1 个答案:

答案 0 :(得分:0)

首先,添加:

- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
    [super traitCollectionDidChange:previousTraitCollection];
    if ((self.traitCollection.verticalSizeClass != previousTraitCollection.verticalSizeClass)
        || (self.traitCollection.horizontalSizeClass != previousTraitCollection.horizontalSizeClass)) {
        [self setupToolbar:self.traitCollection.horizontalSizeClass];
    }
}

然后重新创建工具栏项:

    if (horizontalSizeClass == UIUserInterfaceSizeClassCompact) {
        mainItems = [NSArray arrayWithObjects:..., nil];
    } else {
        mainItems = [NSArray arrayWithObjects:..., nil];
    }
    [mainToolbar setItems:mainItems animated:NO];