gpt4 book ai didi

ios - tabBar.topAnchor 的奇怪布局

转载 作者:行者123 更新时间:2023-11-28 14:56:44 25 4
gpt4 key购买 nike

我有几个 subview

    view.addSubview(collectionView)
view.addSubview(pageControl)
view.addSubview((tabBarController?.tabBar)!)
view.addSubview(skipButton)
view.addSubview(nextButton)

我尝试通过 layoutAnchor 进行布局。 (此时我将 pageControl.bottomAnchor 锚定到 view.bottomAnchor )

   nextButton.anchorWithConstantsToTop(view.topAnchor, left: nil, bottom: nil, right:
view.rightAnchor, topConstant: 16, leftConstant: 0, bottomConstant: 0, rightConstant: 0)
nextButtonTopAnchor = nextButton.topAnchor.constraint(equalTo: view.topAnchor, constant: 0)
nextButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
nextButton.widthAnchor.constraint(equalToConstant: 60).isActive = true
skipButton.anchorWithConstantsToTop(view.topAnchor, left: view.leftAnchor, bottom: nil, right: nil, topConstant: 16, leftConstant: 0, bottomConstant: 0, rightConstant: 0)
skipButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
skipButton.widthAnchor.constraint(equalToConstant: 60).isActive = true
pageControl.anchorWithConstantsToTop(nil, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, topConstant: 0, leftConstant: 28, bottomConstant: 0, rightConstant: 30)

所以它工作得很好,当我旋转设备 pageControl 夹在适当的位置时 enter image description here

但是如果尝试锚定 pageControl.bottomAnchor = tabBarController?.tabBar.topAnchor 而不是 view.bottomAnchor 那么在纵向方向上一切正常,但是当我旋转设备时会发生一些令人恼火的事情:pageControl 上升 enter image description here

此外,当我从横向方向恢复为纵向方向时它保持在上方位置enter image description here

我猜问题出在 pageControl.bottomAnchor = tabBarController?.tabBar.topAnchor string 上,但我知道为什么?在横向仍然是 tabBar,pageControl 仍然必须将它锚定到 tabBar 的 topAnchor。请问有人知道这种行为是怎么回事吗?

最佳答案

问题出在这一行:

view.addSubview((tabBarController?.tabBar)!)

你不应该那样做。请删除它,然后在当前 View Controller 添加到 UITabBarController 后设置您的约束。第一次执行 viewWillAppear 时可能会出现这种情况。

如果您的 pageControl 是以编程方式创建的,那么您还应该将其 translatesAutoresizingMaskIntoConstraints 属性设置为 false(如评论中所指出的)。如果控件是在 Storyboard中创建的,则不需要此步骤。

关于ios - tabBar.topAnchor 的奇怪布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49283558/

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