gpt4 book ai didi

ios - 具有正偏移量的 CenterXAnchor 不起作用?

转载 作者:行者123 更新时间:2023-11-28 15:08:30 25 4
gpt4 key购买 nike

我在 ViewController 中创建了一个 View 并创建了一个 Outlet。该 View 称为“chatView”。我想在主视图加载后直接隐藏 View (并在一段时间后,当用户单击按钮时将其滑入)。

我的方法是操纵 centerXAnchor 约束:

override func viewDidLoad() {
super.viewDidLoad()

view.insertSubview(chatView, belowSubview: view)
chatView.translatesAutoresizingMaskIntoConstraints = false
chatCenterX = chatView.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 1500)
chatCenterX.isActive = true
}

但是由于某种原因(我不知道为什么),加载 View 时已经显示了 chatView,因此未设置偏移量。

我尝试了一些其他方法,发现如果将偏移量设置为负值(我尝试使用 -1500 而不是 1500),偏移量会起作用。

你知道我做错了什么吗?

最佳答案

将您在 Storyboard 中添加的约束作为 IBOutlet 拖动并更改其常量值以根据需要向前或向后移动 View

注意:在 viewDidLayoutSubviews 中更改它,我首先使用 bool 值启动,这样当你想显示它时就不会被隐藏,而不是在 viewDidLoad 中

Apple 说:

viewDidLayoutSubviews()

When the bounds change for a view controller's view, the view adjusts the positions of its subviews and then the system calls this method. However, this method being called does not indicate that the individual layouts of the view's subviews have been adjusted. Each subview is responsible for adjusting its own layout.

Your view controller can override this method to make changes after the view lays out its subviews. The default implementation of this method does nothing.

因此每次都会调用 viewDidLayoutSubviews,这就是我们向函数添加 bool 变量的原因。

override func viewDidLayoutSubviews
{
if(once)
{
once = false
self.chatViewCenterX.constant = 1500
self.view.layoutIfNeeded()
}
}

在任何地方再次显示

 self.chatViewCenterX.constant = 0

self.view.layoutIfNeeded() // viewDidLayoutSubviews is called here.

关于ios - 具有正偏移量的 CenterXAnchor 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48053217/

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