当我改变设备的方向时,为什么框架中的值等于特定大小不做同样的工作?
Why value in the frame than is equal to a specific size doesn't do the same job when I change the orientation of the device?
我有一段代码允许我在更改设备方向时更改 UIImageView
的大小。
我硬编码了这些值(高度 = 896 和宽度 = 414),它运行良好,但是当我想通过放置动态值(height = self.view.frame.size.height
和 width = 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!)
有什么想法吗?
在那个方法中放置一个断点,你会看到这个方法在视图改变方向之前被调用,这就是为什么你没有正确的高度和宽度,而是使用 size 参数中的值,这个应该可以。
但是同意评论,改用auto-layout,它会让你的生活更轻松
如果您想调整导航栏的大小
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!)
之后
我有一段代码允许我在更改设备方向时更改 UIImageView
的大小。
我硬编码了这些值(高度 = 896 和宽度 = 414),它运行良好,但是当我想通过放置动态值(height = self.view.frame.size.height
和 width = 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!)
有什么想法吗?
在那个方法中放置一个断点,你会看到这个方法在视图改变方向之前被调用,这就是为什么你没有正确的高度和宽度,而是使用 size 参数中的值,这个应该可以。
但是同意评论,改用auto-layout,它会让你的生活更轻松
如果您想调整导航栏的大小
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!)
之后