gpt4 book ai didi

iOS:UIView 原点在 iOS 9 上没有改变

转载 作者:行者123 更新时间:2023-11-30 11:36:52 24 4
gpt4 key购买 nike

我试图在显示键盘时设置我的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
}

关于iOS:UIView 原点在 iOS 9 上没有改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49661981/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com