gpt4 book ai didi

Swift - 以编程方式刷新约束

转载 作者:行者123 更新时间:2023-12-04 04:17:04 24 4
gpt4 key购买 nike

我的 VC 开始于 stackViewAlign Bottom to Safe Area .

我有 tabBar,但一开始是隐藏的 tabBar.isHidden = true .

稍后当 tabBar 出现时,它隐藏了 stackView
所以我需要在 tabBar.isHidden = false 之后刷新约束的函数

当我用 tabBar.isHidden = false 启动应用程序时stackView正确显示。

尝试了每个功能,如:stackView.needsUpdateConstraints() , updateConstraints() , setNeedsUpdateConstraints()没有成功。

现在我正在以编程方式更改底部,但是当我切换 tabBarIndex 并返回到更改了底部约束的那个时,它会检测到 tabBar 并在另一个 View (未附加约束)下提升 stackView。喜欢再次刷新约束。我正在隐藏和显示此 stackView 并限制屏幕开/关。

我需要在 tabBar.isHidden = false 之后刷新约束,但约束没有检测到 tabBar 的外观。

正如我提到在 tabBars 之间切换修复了这个问题,所以一些代码在切换后执行以检测 tabBar。有人知道这个代码吗?我尝试调用方法 viewDidLayoutSubviews 和 viewWillLayoutSubviews 没有成功......有什么建议吗?

最佳答案

这种业余方法修复了我的错误...:D

tabBarController!.selectedIndex = 1
tabBarController!.selectedIndex = 0

或者带有扩展名
extension UITabBarController {

// Basically just toggles the tabs to fix layout issues
func forceConstraintRefresh() {

// Get the indices we need
let prevIndex = selectedIndex
var newIndex = 0

// Find an unused index
let items = viewControllers ?? []
find: for i in 0..<items.count {
if (i != prevIndex) {
newIndex = i
break find
}
}

// Toggle the tabs
selectedIndex = newIndex
selectedIndex = prevIndex

}

}
用法(在切换暗/亮模式时调用):
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)

tabBarController?.forceConstraintRefresh()

}

关于Swift - 以编程方式刷新约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60526419/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com