iOS: UIView 原点 y 在 iOS 9 上没有改变
iOS: UIView origin y not changing on iOS 9
我正在尝试在显示 keyboard
时设置我的 self.mainView.frame.origin.y
。它在 iOS 11
上运行良好,但在 iOS 9
上运行良好。
这是我的代码:
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
print("=== textFieldShouldBeginEditing 1 \(self.mainView.frame.origin.y) ")
self.mainView.frame.origin.y -= 150
self.mainView.setNeedsLayout()
self.mainView.layoutIfNeeded()
print("=== textFieldShouldBeginEditing 2 \(self.mainView.frame.origin.y) ")
return true
}
internal func textFieldShouldReturn(_ textField: UITextField) -> Bool {
print("=== textFieldShouldReturn 1 \(self.mainView.frame.origin.y) ")
self.mainView.frame.origin.y = self.normalMainViewOriginY
textField.endEditing(true)
print("=== textFieldShouldReturn 2 \(self.mainView.frame.origin.y) ")
return true
}
在viewDidLoad()
:
self.normalMainViewOriginY = self.mainView.frame.origin.y
我也使用 NSNotification
测试了同样的想法,但我在 iOS 9
中遇到了同样的问题
这些是打印输出:
iOS 9:
=== normalMainViewOriginY:243.0(来自viewDidLoad()
)
=== textFieldShouldBeginEditing 1: 208.5
=== textFieldShouldBeginEditing 2: 208.5
=== textFieldShouldReturn 1: 208.5
=== textFieldShouldReturn 2: 243.0
iOS 11:
=== normalMainViewOriginY:243.0(来自viewDidLoad()
)
=== textFieldShouldBeginEditing 1: 208.5
=== textFieldShouldBeginEditing 2: 58.5
=== textFieldShouldReturn 1: 58.5
=== textFieldShouldReturn 2: 243.0
问题是改变框架不会影响下划线布局约束,所以尝试约束它不会失败,假设你有 mainView 的顶部约束 mainViewTopCon
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
print("=== textFieldShouldBeginEditing 1 \(self.mainView.frame.origin.y) ")
self.mainViewTopCon.constant -= 150
self.mainView.layoutIfNeeded()
print("=== textFieldShouldBeginEditing 2 \(self.mainView.frame.origin.y) ")
return true
}
internal func textFieldShouldReturn(_ textField: UITextField) -> Bool {
print("=== textFieldShouldReturn 1 \(self.mainView.frame.origin.y) ")
self.mainViewTopCon.constant = 0 // or value in IB that you set initally
self.mainView.layoutIfNeeded()
textField.endEditing(true)
print("=== textFieldShouldReturn 2 \(self.mainView.frame.origin.y) ")
return true
}
我正在尝试在显示 keyboard
时设置我的 self.mainView.frame.origin.y
。它在 iOS 11
上运行良好,但在 iOS 9
上运行良好。
这是我的代码:
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
print("=== textFieldShouldBeginEditing 1 \(self.mainView.frame.origin.y) ")
self.mainView.frame.origin.y -= 150
self.mainView.setNeedsLayout()
self.mainView.layoutIfNeeded()
print("=== textFieldShouldBeginEditing 2 \(self.mainView.frame.origin.y) ")
return true
}
internal func textFieldShouldReturn(_ textField: UITextField) -> Bool {
print("=== textFieldShouldReturn 1 \(self.mainView.frame.origin.y) ")
self.mainView.frame.origin.y = self.normalMainViewOriginY
textField.endEditing(true)
print("=== textFieldShouldReturn 2 \(self.mainView.frame.origin.y) ")
return true
}
在viewDidLoad()
:
self.normalMainViewOriginY = self.mainView.frame.origin.y
我也使用 NSNotification
测试了同样的想法,但我在 iOS 9
这些是打印输出:
iOS 9:
=== normalMainViewOriginY:243.0(来自viewDidLoad()
)
=== textFieldShouldBeginEditing 1: 208.5
=== textFieldShouldBeginEditing 2: 208.5
=== textFieldShouldReturn 1: 208.5
=== textFieldShouldReturn 2: 243.0
iOS 11:
=== normalMainViewOriginY:243.0(来自viewDidLoad()
)
=== textFieldShouldBeginEditing 1: 208.5
=== textFieldShouldBeginEditing 2: 58.5
=== textFieldShouldReturn 1: 58.5
=== textFieldShouldReturn 2: 243.0
问题是改变框架不会影响下划线布局约束,所以尝试约束它不会失败,假设你有 mainView 的顶部约束 mainViewTopCon
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
print("=== textFieldShouldBeginEditing 1 \(self.mainView.frame.origin.y) ")
self.mainViewTopCon.constant -= 150
self.mainView.layoutIfNeeded()
print("=== textFieldShouldBeginEditing 2 \(self.mainView.frame.origin.y) ")
return true
}
internal func textFieldShouldReturn(_ textField: UITextField) -> Bool {
print("=== textFieldShouldReturn 1 \(self.mainView.frame.origin.y) ")
self.mainViewTopCon.constant = 0 // or value in IB that you set initally
self.mainView.layoutIfNeeded()
textField.endEditing(true)
print("=== textFieldShouldReturn 2 \(self.mainView.frame.origin.y) ")
return true
}