视图旋转时视图尺寸变小
the view size gets smaller when the view is rotated
我有意见。旋转时,视图尺寸变小。你能看看我附上的照片吗?
@IBAction func buttonForNewView(_ sender: Any) {
self.view.addSubview(customvView)
customvView.layer.cornerRadius = 15
customvView.backgroundColor = .gray
customvView.translatesAutoresizingMaskIntoConstraints = false
customvView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: view.frame.size.height*0.1).isActive = true
customvView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: view.frame.size.width*0.1).isActive = true
customvView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -view.frame.size.width*0.1).isActive = true
customvView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant:-view.frame.size.height*0.1).isActive = true
}
检查此代码。
let val = view.frame.size.height*0.1
self.view.addSubview(customvView)
customvView.layer.cornerRadius = 15
customvView.backgroundColor = .gray
customvView.translatesAutoresizingMaskIntoConstraints = false
customvView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: val).isActive = true
customvView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: val).isActive = true
customvView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -val).isActive = true
customvView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant:-val).isActive = true
旋转设备时不会重新计算约束。
这将约束计算分解为一个单独的函数。然后它会覆盖 traitCollectionDidChange()。当特征集合发生变化时,它会删除旧的约束(以防止自动布局混淆)并重新应用新的约束。
import UIKit
class ViewController: UIViewController {
var customvView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
customvView = UIView()
// Do any additional setup after loading the view.
}
func constrainCustomvView() {
customvView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: view.frame.size.height*0.1).isActive = true
customvView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: view.frame.size.width*0.1).isActive = true
customvView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -view.frame.size.width*0.1).isActive = true
customvView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant:-view.frame.size.height*0.1).isActive = true
}
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
if view.subviews.contains(customvView) { // for the case that the traits change before the view is created
// remove the existing constraits, or autolayout will have to choose between two and it will choose poorly
for constraint in view.constraints {
if let first = constraint.firstItem as? UIView, first == customvView {
view.removeConstraint(constraint)
}
if let second = constraint.secondItem as? UIView, second == customvView {
view.removeConstraint(constraint)
}
}
constrainCustomvView()
}
}
@IBAction func CustomButton(_ sender: Any) {
self.view.addSubview(customvView)
customvView.layer.cornerRadius = 15
customvView.backgroundColor = .gray
customvView.translatesAutoresizingMaskIntoConstraints = false
constrainCustomvView()
}
}
我有意见。旋转时,视图尺寸变小。你能看看我附上的照片吗?
@IBAction func buttonForNewView(_ sender: Any) {
self.view.addSubview(customvView)
customvView.layer.cornerRadius = 15
customvView.backgroundColor = .gray
customvView.translatesAutoresizingMaskIntoConstraints = false
customvView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: view.frame.size.height*0.1).isActive = true
customvView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: view.frame.size.width*0.1).isActive = true
customvView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -view.frame.size.width*0.1).isActive = true
customvView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant:-view.frame.size.height*0.1).isActive = true
}
检查此代码。
let val = view.frame.size.height*0.1
self.view.addSubview(customvView)
customvView.layer.cornerRadius = 15
customvView.backgroundColor = .gray
customvView.translatesAutoresizingMaskIntoConstraints = false
customvView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: val).isActive = true
customvView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: val).isActive = true
customvView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -val).isActive = true
customvView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant:-val).isActive = true
旋转设备时不会重新计算约束。
这将约束计算分解为一个单独的函数。然后它会覆盖 traitCollectionDidChange()。当特征集合发生变化时,它会删除旧的约束(以防止自动布局混淆)并重新应用新的约束。
import UIKit
class ViewController: UIViewController {
var customvView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
customvView = UIView()
// Do any additional setup after loading the view.
}
func constrainCustomvView() {
customvView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: view.frame.size.height*0.1).isActive = true
customvView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: view.frame.size.width*0.1).isActive = true
customvView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -view.frame.size.width*0.1).isActive = true
customvView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant:-view.frame.size.height*0.1).isActive = true
}
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
if view.subviews.contains(customvView) { // for the case that the traits change before the view is created
// remove the existing constraits, or autolayout will have to choose between two and it will choose poorly
for constraint in view.constraints {
if let first = constraint.firstItem as? UIView, first == customvView {
view.removeConstraint(constraint)
}
if let second = constraint.secondItem as? UIView, second == customvView {
view.removeConstraint(constraint)
}
}
constrainCustomvView()
}
}
@IBAction func CustomButton(_ sender: Any) {
self.view.addSubview(customvView)
customvView.layer.cornerRadius = 15
customvView.backgroundColor = .gray
customvView.translatesAutoresizingMaskIntoConstraints = false
constrainCustomvView()
}
}