在 viewDidLoad 中更改视图位置
Change View Positing in viewDidLoad
我正在尝试使用以下代码通过 viewDidLoad
在应用程序启动时更改单独 View
的位置:
let xPosition = dynView.frame.origin.x - 100
//View will slide view
let yPosition = dynView.frame.origin.y;
let height = dynView.frame.size.height
let width = dynView.frame.size.width
UIView.animate(withDuration: 1.0, animations: {
self.dynView.frame = CGRect(x: xPosition, y: yPosition, width: width, height: height)
})
但是,如果我使用 @IBAction
分配给按钮,这似乎无法直接在 viewDidLoad
代码中工作
我想我在这里遗漏了什么,但我不知道是什么。
参考您在评论中关于使用约束的回答:
Yes, I do. I'm assuming this has something to do with it? Is the
constraints loaded after the viewDidLoad?
没错,来自 viewDidLoad() 文档:
Called after the controller's view is loaded into memory.
与检查约束设置是否已完成(子视图布局)无关。
如果我没记错的话,你要找的是 viewDidLayoutSubviews() 方法:
Called to notify the view controller that its view has just laid out
its subviews.
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.
因此,不要在 viewDidLoad()
中实现更改视图的 y 位置的代码,而是尝试让它在 viewDidLayoutSubviews()
中实现,如下所示:
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let xPosition = dynView.frame.origin.x - 100
let yPosition = dynView.frame.origin.y
let height = dynView.frame.size.height
let width = dynView.frame.size.width
UIView.animate(withDuration: 1.0, animations: {
self.dynView.frame = CGRect(x: xPosition, y: yPosition, width: width, height: height)
})
}
另外:
如果不想让动画在popping/dismissing下一个ViewController之后重复播放,可以声明一个flag 属性来判断是否启动动画,类似:
var isAnimated = false
override func viewDidLayoutSubviews() {
if isAnimated == false {
super.viewDidLayoutSubviews()
UIView.animate(withDuration: 1.0, animations: {
self.btn.frame.origin.y = 20
}, completion: { _ in
self.isAnimated = true
})
}
}
补充说明:
我建议更改约束的值而不是直接更改框架 size/origin。您可能需要查看 this question/answer.
我正在尝试使用以下代码通过 viewDidLoad
在应用程序启动时更改单独 View
的位置:
let xPosition = dynView.frame.origin.x - 100
//View will slide view
let yPosition = dynView.frame.origin.y;
let height = dynView.frame.size.height
let width = dynView.frame.size.width
UIView.animate(withDuration: 1.0, animations: {
self.dynView.frame = CGRect(x: xPosition, y: yPosition, width: width, height: height)
})
但是,如果我使用 @IBAction
viewDidLoad
代码中工作
我想我在这里遗漏了什么,但我不知道是什么。
参考您在评论中关于使用约束的回答:
Yes, I do. I'm assuming this has something to do with it? Is the constraints loaded after the viewDidLoad?
没错,来自 viewDidLoad() 文档:
Called after the controller's view is loaded into memory.
与检查约束设置是否已完成(子视图布局)无关。
如果我没记错的话,你要找的是 viewDidLayoutSubviews() 方法:
Called to notify the view controller that its view has just laid out its subviews.
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.
因此,不要在 viewDidLoad()
中实现更改视图的 y 位置的代码,而是尝试让它在 viewDidLayoutSubviews()
中实现,如下所示:
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let xPosition = dynView.frame.origin.x - 100
let yPosition = dynView.frame.origin.y
let height = dynView.frame.size.height
let width = dynView.frame.size.width
UIView.animate(withDuration: 1.0, animations: {
self.dynView.frame = CGRect(x: xPosition, y: yPosition, width: width, height: height)
})
}
另外: 如果不想让动画在popping/dismissing下一个ViewController之后重复播放,可以声明一个flag 属性来判断是否启动动画,类似:
var isAnimated = false
override func viewDidLayoutSubviews() {
if isAnimated == false {
super.viewDidLayoutSubviews()
UIView.animate(withDuration: 1.0, animations: {
self.btn.frame.origin.y = 20
}, completion: { _ in
self.isAnimated = true
})
}
}
补充说明:
我建议更改约束的值而不是直接更改框架 size/origin。您可能需要查看 this question/answer.