2017-09-13 87 views
1

我創建了一個UIToolBar以編程方式,它工作正常,除了iPhone X,我希望工具欄根據手機的方向展開。如果工具欄是使用界面構建器創建的,則擴展工作。iOS 11 UIToolBar自動擴展

我沒有設置任何高度限制,甚至隱式地通過自動佈局遮罩。

代碼:

UIToolbar* toolbar = [[UIToolbar alloc] init]; 
toolbar.translatesAutoresizingMaskIntoConstraints = NO; 
[self.view addSubview:toolbar]; 
[self.view addConstraints:@[ 
          [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:toolbar attribute:NSLayoutAttributeLeft multiplier:1 constant:0], 
          [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:toolbar attribute:NSLayoutAttributeBottom multiplier:1 constant:0], 
          [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:toolbar attribute:NSLayoutAttributeRight multiplier:1 constant:0]]]; 

所以,我應該怎麼辦要使工具欄的行爲是否正確?

回答

1

確定,所述部分的答案是使用安全區self.view.safeAreaLayoutGuide,或與上下文:

[NSLayoutConstraint constraintWithItem:self.view.safeAreaLayoutGuide attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:toolbar attribute:NSLayoutAttributeBottom multiplier:1 constant:0], 

的「部分」部分指的是IB工具欄具有49的高度,和手動工具欄上有44的事實

這對我來說已經夠用了,但如果有人能夠解決/解釋44-49的差異,我會將其標記爲解決方案。

編輯:我找到了缺少的部分。我不打invalidateIntrinsicContentSize。從willTransitionToTraitCollection: withTransitionCoordinator:添加一個電話固定。不知何故,在去年左右,我錯過了工具欄在緊湊模式下改變其大小。

+0

我相信底部工具欄中的新高度差異是爲了解釋屏幕底部的「抓取條」,以指導用戶「輕掃回家」的姿勢。 –

+0

@BenKreeger是的,這是由我寫的。我不知道的是如何處理這個事實,即使固有尺寸是49,但約束條件有時仍然保持在44.並且對於XIB似乎不會發生。 – Rychu