gpt4 book ai didi

ios - 当我改变设备的方向时,为什么框架中的值等于特定大小不做同样的工作?

转载 作者:搜寻专家 更新时间:2023-11-01 06:52:32 25 4
gpt4 key购买 nike

我有一段代码允许我在设备方向更改期间更改 UIImageView 的大小。

我对这些值进行了硬编码(高度 = 896 和宽度 = 414),并且效果很好,但是当我想通过放置动态值(height = self.view.frame.size. heightwidth = self.view.frame.size.width), 它不起作用...

但是,self.view.frame.size.height = 896 和 self.view.frame.size.width = 414 这很奇怪...我错过了我的想法。

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

if UIDevice.current.orientation.isLandscape {
print("Landscape")
self.newImageView?.frame.size.height = 414
self.newImageView?.frame.size.width = 896
navBar?.frame = CGRect(x: 0, y: 0, width: 896, height: 100)
} else {
print("Portrait")
self.newImageView?.frame.size.height = 896
self.newImageView?.frame.size.width = 414
navBar?.frame = CGRect(x: 0, y: 40, width: 414, height: 100)
}
}

编辑(解决方案):有关信息,我在我的代码中使用了这种方式,但票证中的解决方案更短、更现代

 //Share bar
let shareBar: UIBarButtonItem = UIBarButtonItem.init(barButtonSystemItem:.action, target: self, action: #selector(userDidTapShare))
self.navigationItem.rightBarButtonItem = shareBar
if UIDevice.current.orientation.isLandscape {
let width = max(self.view.frame.size.width, self.view.frame.size.height)
let height = min(self.view.frame.size.width, self.view.frame.size.height)
self.newImageView?.frame.size.height = height
self.newImageView?.frame.size.width = width
navBar = UINavigationBar(frame: CGRect(x: 0, y: UIApplication.shared.statusBarFrame.height, width: width, height: 100))
} else {
let width = min(self.view.frame.size.width, self.view.frame.size.height)
let height = max(self.view.frame.size.width, self.view.frame.size.height)
self.newImageView?.frame.size.height = height
self.newImageView?.frame.size.width = width
navBar = UINavigationBar(frame: CGRect(x: 0, y: UIApplication.shared.statusBarFrame.height, width: width, height: 100))
}
view.addSubview(navBar!)

有什么想法吗?

最佳答案

如果你想调整导航栏的大小

 self.navBar.autoresizingMask = (UIView.AutoresizingMask(rawValue: UIView.AutoresizingMask.RawValue(UInt8(UIView.AutoresizingMask.flexibleRightMargin.rawValue) | UInt8(UIView.AutoresizingMask.flexibleLeftMargin.rawValue) | UInt8(UIView.AutoresizingMask.flexibleBottomMargin.rawValue) | UInt8(UIView.AutoresizingMask.flexibleTopMargin.rawValue))))

把它放在你的 view.addSubview(navBar!) 之后

关于ios - 当我改变设备的方向时,为什么框架中的值等于特定大小不做同样的工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56131603/

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