gpt4 book ai didi

ios - 根据方向调整弹出窗口大小

转载 作者:行者123 更新时间:2023-11-29 05:42:57 25 4
gpt4 key购买 nike

我的任务是根据设备方向制作一个可调整大小的弹出窗口。我像这样在窗口上实例化它。

let window = UIApplication.shared.keyWindow
let popOver = DeletePopUpViewController(nibName: "DeletePopUpViewController", bundle: nil)
popOver?.loadViewIfNeeded()
popOver?.view.frame = window!.frame
window?.addSubview(popOver!.view)

我在 IB 中添加了约束,因为我现在的任务不是调整大小。现在我需要将约束转移到代码中,这就是困扰我的地方。我能够用像这样的观察者检测方向变化 -

NotificationCenter.default.addObserver(self, selector: #selector(rotated), name: UIDevice.orientationDidChangeNotification, object: nil)

@objc private func rotated() {
if UIDevice.current.orientation == UIDeviceOrientation.landscapeLeft || UIDevice.current.orientation == UIDeviceOrientation.landscapeRight {
//code to implement constraints and size in landscape.
}
if UIDevice.current.orientation == UIDeviceOrientation.portrait || UIDevice.current.orientation == UIDeviceOrientation.portraitUpsideDown {
//code to implement constraints and size in portrait.
}
}

您能给我一些从哪里开始的指导吗,因为我对 Swift 还很陌生,并且没有在代码中做很多限制。另外文章也会有帮助。基本上每一个帮助都是受欢迎的。

提前致谢。

最佳答案

您需要将其添加到您的弹出 View 中,它将自动调整大小

let popOver = DeletePopUpViewController(nibName:   "DeletePopUpViewController", bundle: nil)
popOver?.loadViewIfNeeded()
popOver?.autoresizingMask = [.flexibleHeight, .flexibleWidth, .flexibleTopMargin, .flexibleRightMargin, .flexibleLeftMargin, .flexibleBottomMargin]
popOver?.view.frame = window!.frame
window?.addSubview(popOver!.view)

关于ios - 根据方向调整弹出窗口大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56389173/

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