我正在尝试一个非常基本的应用程序,其中有一个自定义菜单栏。菜单栏应该占据屏幕的1/4。而且由于必须在方向更改时重新计算此值,因此每次方向更改时我都会再次设置菜单栏。但是问题是,当我最初以纵向模式打开应用程序时,菜单栏设置正确,即使更改为横向,它仍然可以,但是当我从横向转换为肖像时,由于某种原因,它不能正确执行并增加菜单栏的高度。我想念什么吗?
下面是我的UIViewController
class HomeViewController: UIViewController{
var metrics : [String : CGFloat] = [:]
let table = OptionsTable()
let menuBar = MenuBar()
var views : [UIView] = []
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.barTintColor = UIColor.getColorFromHex(hexCode: "1B9AF7")
navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
setUpMenuBar()
}
private func setUpMenuBar(){
views += [menuBar]
view.addSubview(menuBar)
let bottomPaddingForMenuBar = view.frame.height - (view.frame.height/4)
print("Padding \(bottomPaddingForMenuBar)")
metrics.updateValue(bottomPaddingForMenuBar, forKey: "bottomPaddingFormenuBar")
view.addConstraintWithFormat("H:|[v0]|", views: views, metrics: metrics)
view.addConstraintWithFormat("V:|-0@750-[v0]-bottomPaddingFormenuBar@750-|", views: views, metrics: metrics)
}
override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
setUpMenuBar()
if fromInterfaceOrientation == .portrait || fromInterfaceOrientation == .portraitUpsideDown {
print("Landscape")
}else{
print("Portrait")
}
}
下面是我的addConstarintWithFormat()
方法
extension UIView{
func addConstraintWithFormat(_ format : String, views : [UIView], metrics : [String : CGFloat]) {
var viewsDictionary = [String : UIView]()
for(index, view) in views.enumerated(){
let key = "v\(index)"
view.translatesAutoresizingMaskIntoConstraints = false
viewsDictionary[key] = view
}
print("duuh\(viewsDictionary["v0"]!)")
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutConstraint.FormatOptions(), metrics: metrics, views: viewsDictionary))
}
预先感谢!