viewDidAppear和viewDidLoad初始化位置太晚了。

3

我想要更新一个在故事板中的UIView(并从故事板实例化)在应用程序加载时。

我需要以动态方式定位几个图标(界面构建器目前还无法让我这样做)。 但是,如果我将我的代码放在viewDidAppear或viewDidLoad中,似乎调用太晚了。

以下是视图加载时的第一两秒钟发生的情况:

initial load

稍后它会到达正确的位置(因为代码被调用)。

Fixed position a second later

我的问题是,我需要在哪里初始化这些对象的位置,以便它们不会在一秒钟后突然跳转。 viewDidLoad和viewDidAppear太晚了吗? 为什么!? :)

override func viewDidAppear(animated: Bool)
{
    initView()
    _gridLines = false

}

func initView()
{
    _cameraFeed.initAfterLoad()
    //center the buttons on half distance between middle button and screen edge
    var middleDistance:CGFloat = _swapButton.frame.origin.x + _swapButton.frame.width/2
    _linesButton.frame.origin.x = middleDistance/2 - _linesButton.frame.width/2
    _flashButton.frame.origin.x = middleDistance + middleDistance/2 - _flashButton.frame.width/2
    _selectPhotos.frame.origin.x = middleDistance/2 - _selectPhotos.frame.width/2
}

欢迎提供Swift和Objective-C的解决方案!


我在你的第二张图片中看不到任何不能用IB约束条件设置的内容。那么,IB不允许你做什么呢? - rdelmar
您可以在此处查看问题:http://stackoverflow.com/questions/26477049/how-to-get-leading-space-to-be-1-3-the-width-of-the-superview - Aggressor
我回答了那个问题。 - rdelmar
1个回答

4
尝试将设置框架的代码放在viewWillAppear而不是viewDidAppear中。 此外,您应该调用super。
override func viewWillAppear(animated: Bool) {
    initView()
    _gridLines = false

    super.viewWillAppear(animated)
}

很遗憾,这次没有起作用 :(. 实际上,它甚至没有移动图标,它们只是在右侧冻结了。 - Aggressor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接