【问题标题】:Cannot move UITextView programmatically无法以编程方式移动 UITextView
【发布时间】:2016-03-08 08:47:27
【问题描述】:

这是我的问题,我想为我的登录屏幕制作动画,这是一个基本屏幕、2 个 UITextView 和 2 个按钮(登录和注册)

我想让我的用户名字段在应用程序启动时出现在屏幕外并为其设置动画,所以这是我的代码:

override func viewWillAppear(animated: Bool) {
    self.navigationController?.navigationBarHidden = true
    print (usernameField.center.x)

    usernameField.center.x -= view.bounds.width
    print (view.bounds.width)
    print (usernameField.center.x)

}

这是输出:

300.0
375.0
-75.0

所以我的 usernameField 的最后一个 x 是 -75.0,但在所有情况下它都位于我的应用程序的中心。

我认为这是因为我在 Storyboard 中对其应用了约束? (建议将按钮和文本视图居中的约束)

感谢您的帮助

编辑: 删除约束并没有解决我的问题..

【问题讨论】:

  • 尝试在 viewDidLayoutSubviews 中进行所有与帧相关的计算。顺便说一句,如果您使用约束,请不要尝试更改视图的框架,因为此更改会被忽略或产生意外行为。

标签: ios autolayout swift2 uiviewanimation


【解决方案1】:

您需要保持对保持前导空格与直接邻居/边距的约束的引用。然后为此约束的常量属性设置适当的值。然后要制作动画,您需要调用 动画块中的 layoutIfNeeded 方法。

首先确保您的视图超出边界并位于视图的左侧。

有一个约束的出口说-

@IBOutlet weak var constraint : NSLayoutConstraint!

override func animateIn(){
  //Do any pending layout
  self.view.layoutIfNeeded()
  constraint.constant += 100 //<-- set appropriate value for your case
  //Animate the constraint change to bring in the button into the screen

  UIView.animateWithDuration(0.2, animations: {
    self.view.layoutIfNeeded()
})

}

参考: How do I animate constraint changes?

【讨论】:

    猜你喜欢
    • 2010-11-13
    • 2011-03-11
    • 2011-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-28
    • 1970-01-01
    • 2018-02-18
    相关资源
    最近更新 更多