簡體   English   中英

設備旋轉時的UIToolbar高度

[英]UIToolbar height on device rotation

來自iOS人機界面指南, iOS UI元素使用指南

在iPhone上,考慮設備旋轉時工具欄高度的自動更改 特別是,請確保您的自定義工具欄圖標非常適合橫向顯示的細條。 不要以編程方式指定工具欄的高度。

我可以看到MailTwitter for iPhoneDropbox的高度從44點變為32點,但是當我添加一個工具欄(使用Interface Builder)並讓我的UIViewController子類自動旋轉( shouldAutorotateToInterfaceOrientation:返回YES)時,工具欄不會在設備旋轉時自動更改其高度。

UIToolbar類參考沒有提到這種高度的自動更改,所以我應該以編程方式更改它,即使HIG說不要以編程方式指定工具欄的高度

您是否檢查了工具欄的自動調整大小屬性?

正如yonelGeorge7KV7回答的評論中指出的那樣 ,更改自動調整遮罩在iOS 5上無法正常工作。

我通過使用- [UIView sizeThatFits:]方法找到了另一種解決方案。 我是這樣做的:

- (void) layoutForInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Adjust the toolbar height depending on the screen orientation
    CGSize toolbarSize = [self.toolbar sizeThatFits:self.view.bounds.size];
    self.toolbar.frame = (CGRect){CGPointMake(0.f, CGRectGetHeight(self.view.bounds) - toolbarSize.height), toolbarSize};
    self.webView.frame = (CGRect){CGPointZero, CGSizeMake(CGRectGetWidth(self.view.bounds), CGRectGetMinY(self.toolbar.frame))};
}

然后我在視圖控制器viewWillAppear:willAnimateRotationToInterfaceOrientation:duration: methods中調用這個layoutForInterfaceOrientation: willAnimateRotationToInterfaceOrientation:duration:方法。

盡管如此,高度仍以編程方式更改,但至少該值不是硬編碼的。

使用自動布局實現此行為要簡單得多。 我只在iOS8中進行過測試,但以下代碼適用於方向更改所需的動畫:

public override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    toolbar.invalidateIntrinsicContentSize()
}

如果在NavigationController中使用ViewController,則可以使用NavigationController的工具欄而不是創建自己的工具欄,並讓它處理調整大小。 實際上,這就是ViewController的toolbarItems屬性的toolbarItems

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM