在viewDidLoad中更改视图定位 [英] Change View Positing in viewDidLoad

查看:65
本文介绍了在viewDidLoad中更改视图定位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过viewDidLoad使用以下代码在应用启动时更改单独的View的位置:

I'm trying to change the position of a separate View at launch of the app via the viewDidLoad using this code:

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

我猜我在这里遗漏了一些东西,但是我不知道是什么.

I'm guessing I'm missing something here, but I can't figure out what.

推荐答案

参考您的答案-在注释中-有关使用约束的信息:

Referring to your answer -in the comments- about using constraints:

是的.我假设这与它有关吗?是个 是否在viewDidLoad之后加载了约束?

Yes, I do. I'm assuming this has something to do with it? Is the constraints loaded after the viewDidLoad?

是的,从 viewDidLoad()文档中:

That's right, from viewDidLoad() documentation:

在控制器的视图加载到内存后调用.

Called after the controller's view is loaded into memory.

与检查约束设置是否已完成(布局的子视图)无关.

Nothing to do with checking that the setup of constraints have been done (subviews laid out).

如果我没有记错的话,您要寻找的是 viewDidLayoutSubviews ()方法:

If I'm not mistaking, what are you looking for is the viewDidLayoutSubviews() method:

被调用以通知视图控制器其视图刚刚布局 其子视图.

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()中,如下所示:

So, instead of implementing the code of changing the y position of the view in the viewDidLoad() try to let it in the viewDidLayoutSubviews(), as follows:

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)
    })
}

ALSO: 如果您不希望在弹出/关闭下一个ViewController之后重复播放动画,则可以声明 flag 属性来确定是否开始动画,类似于:

ALSO: If you don't want to let the animation to be repeated after popping/dismissing the next ViewController, you can declare a flag property for determining to start the animation or not, similar to:

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
            })
        }
    }


附加说明:


Additional Note:

我建议更改约束的值,而不是直接更改帧大小/原点.您可能要检查此问题/答案.

I recommend changing the value of constraints instead of directly change the frame size/origin. You might want to check this question/answer.

这篇关于在viewDidLoad中更改视图定位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆