gpt4 book ai didi

ios - viewWillTransition 给出了错误的大小

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:26:10 29 4
gpt4 key购买 nike

我正在制作自定义键盘。我想根据 View 的宽度和高度生成键(UIButtons)的宽度和高度。

最初加载键盘时,viewDidAppear 会正确确定 View 的高度。 (375 x 216)

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.renderKeys()
}

enter image description here

当旋转到横向时,它会调用 viewWillTransition

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)

NSLog("toSize \(size.width) x \(size.height)")

coordinator.animate(alongsideTransition: {(_ context: UIViewControllerTransitionCoordinatorContext) -> Void in
NSLog("animating \(self.view.frame.width) x \(self.view.frame.height)")

}, completion: {(_ context: UIViewControllerTransitionCoordinatorContext) -> Void in

NSLog("animationCompleted \(self.view.frame.width) x \(self.view.frame.height)")
self.renderKeys()
})

}

enter image description here

旋转回纵向结果。

enter image description here

我发现 viewWillTransition 的大小没有正确确定 View 的大小,或者我只是误解了该函数的用法。即使在动画之后,我也无法获得正确的 View 宽度和高度。

这是调试日志的顺序。

[28651:1348399] Calling viewDidAppear

[28651:1348399] Cleaning keys
[28651:1348399] toSize 667.0 x 216.0
[28651:1348399] animating 667.0 x 216.0
[28651:1348399] animationCompleted 667.0 x 216.0

[28651:1348399] Cleaning keys
[28651:1348399] toSize 375.0 x 162.0
[28651:1348399] animating 375.0 x 162.0
[28651:1348399] animationCompleted 375.0 x 162.0

最佳答案

我也在使用自定义键盘,发现了类似的问题。

经过几个小时的实验,我了解到以下内容:

  • 当您将键盘作为应用程序运行时, View 仅加载一次,应用程序生命周期按预期完成(application(didFinishLaunchingWithOptions:)applicationDidBecomeActive ), 等被执行)
  • 所有方向转换都可以在 viewWillTransition(to size:) 中捕获。例如在 iPhone 8+ 中,这将报告大小为 (414.0, 736.0) 或 (736.0, 414.0)

现在,将项目作为键盘目标运行: -

  • 系统不遵循常规的应用生命周期:应用程序(didFinishLaunchingWithOptions:),applicationDidBecomeActive等不报
    • 每次设备从垂直变为水平或反之时,都会加载主 ViewController
    • 尝试在 viewDidLoad 中捕获 viewSize 可能会很棘手,因为您可能需要延迟(使用Timer.scheduledTimer(withTimeInterval:),至少在第一次迭代中
    • updateViewConstraints() 中报告了 viewSize,但该大小似乎并不完全可靠。每次过渡到之后纵向,它可以报告大小为 414 x 736 (ScreenSize) 或 414x 226(正确的键盘尺寸)。如果是 iPhone 8+
    • 考虑到对应用程序生命周期的这种特殊管理,viewWillTransition 方法在从 Portrait 转到时不会执行到横向或从横向到纵向(我想这与每次旋转都会中断的 View 生命周期有关)。
    • 它在从 LandscapeLeft 到 LandscapeRight(通过 UpsideDown,未启用)时执行,并报告正确的尺寸(736 x 162 in aniPhone 8+)

所以,我正在做的是在延迟后在 viewDidLoad 中捕获屏幕尺寸,而忘记 updateViewConstraintsviewWillTransition(to size:)

Timer.scheduledTimer(withTimeInterval: 0.2, repeats: false) { (nil) in
print("Load Size: \(self.view.frame.size)")
}

注意* 这种延迟似乎只在第一次加载时需要(在这种情况下,甚至 updateViewConstraintsviewDidLoad 之前执行。在 viewDidLoad 的后续迭代中,它是不需要。此外。您可能想试验延迟,并在实际设备和模拟器中进行测试

引用表:

设备-(屏幕)-(KeyboardPortrait)-(KeyboardLandscape)

iPhone X - (375 x 812) - (375 x 141) - (662 x 131)

iPhone 8+ - (414 x 736) - (414 x 226) - (736 x 162)

iPhone 7+/8+ - (414 x 736) - (414 x 226) - (736 x 162)

iPhone 7/8 - (375 x 667) - (375 x 216) - (667 x 162)

6/6s/6+/6s+ - 与 iPhone 7/8 相同

iPhone SE - (320 x 568) - (320 x 216) - (568 x 162)

希望对你有所帮助。问候... e

关于ios - viewWillTransition 给出了错误的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41309308/

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