7.5、横屏竖屏导航

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    
    
    //导航条的使用
    //导航视图控制器创建的时候,默认创建了一个导航条UINavigationBar对象
    //我们可以直接使用这个对象
    
    //1)隐藏导航条
    //hidden是UIView的属性
    //hidden==true  -> 视图不显示
    //hidden==false -> 视图显示
    //self.navigationController?.navigationBar.hidden = true
    //跟上面的方式是等价的
    //self.navigationController?.navigationBarHidden = true
    
    //2)设置背景颜色
    //默认背景颜色是半透明的
    //self.navigationController?.navigationBar.backgroundColor = UIColor.greenColor()
    
    //如果需要完整的颜色
    //self.navigationController?.navigationBar.barTintColor = UIColor.greenColor()
    
    //3)设置背景图片
    /*
     第一个参数:图片对象
     第二个参数:区分屏幕的类型
     */
    
    //Default是默认的竖屏
    //导航的高度是44,宽度是屏幕的宽度
    //iOS7之后,导航会扩充到状态栏(20)
    //导航实际是占据了44+20
    //stretchableImageWithLeftCapWidth是对图片进行拉伸
    let image = UIImage(named: "navigationbar")?.stretchableImage(withLeftCapWidth: 0, topCapHeight: 0)
   self.navigationController?.navigationBar.setBackgroundImage(image, for: .default)
    
    //Compact是横屏是的导航背景图片
    //横屏是高度是32,宽度是屏幕宽度
    let image2 = UIImage(named: "nav-32")?.stretchableImage(withLeftCapWidth: 0, topCapHeight: 0)
   self.navigationController?.navigationBar.setBackgroundImage(image2, for: .compact)
    
    
    self.view.backgroundColor = UIColor.red
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

你可能感兴趣的:(7.5、横屏竖屏导航)