我正在處理自定義標簽欄按鈕的東西,想知道如何使用視圖的高度或以編程方式向上移動按鈕。

我有這個加號按鈕,我想從底部向上移動 10pt。
let newButton = UIButton(frame: CGRect(x: 0, y: 0, width: 65, height: 65))
newButton.imageView?.contentMode = .scaleAspectFit
newButton.contentHorizontalAlignment = .fill
newButton.contentVerticalAlignment = .fill
var newButtonFrame = newEventButton.frame
newButtonFrame.origin.y = view.bounds.height - 65 - 10
因此,當 iPhone 沒有主頁按鈕槽口時,上面的代碼有效。就像獲取視圖的高度并減去按鈕高度和 10。
但是,很明顯,當有一個缺口時,它會變成下圖所示的樣子。

我想從選項卡欄底部向上移動按鈕,那么我怎樣才能獲得螢屏的高度直到選項卡欄的底部,這在有/沒有主頁缺口上都有效?
uj5u.com熱心網友回復:
雖然@wonder 的回答完全正確,但仍有一些缺陷。
首先,您不應該使用 frame 來確定這樣的視圖布局。
其次,沒有必要為此進入視窗的 UIApplication 單例。因為它位于父母視圖中。無論是在safeAreaInsets或safeAreaLayoutGuide。
布局指南用于錨點,這比框架更具可持續性。
這里有它的外觀示例代碼:
let newButton = UIButton()
newButton.imageView?.contentMode = .scaleAspectFit
newButton.contentHorizontalAlignment = .fill
newButton.contentVerticalAlignment = .fill
NSLayoutConstraint.activate([
newButton.heightAnchor.constraint(equalTo: 65),
newButton.widthAnchor.constraint(equalTo: 65),
newButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
newButton.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -10)
])
uj5u.com熱心網友回復:
對于按鈕原點使用安全是插入,如果設備有缺口 safeAreaBottom 將像 44 ,否則它將是 0:
let safeAreaBottom = UIApplication.shared.windows.first?.safeAreaInsets.bottom
然后使用它:
newButtonFrame.origin.y = view.bounds.height - safeAreaBottom - 65 - 10
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/345143.html
上一篇:鍵盤覆寫UITextView
