Search code examples
iosswiftuinavigationcontrolleruisegmentedcontroluinavigationitem

Swift : How to change navigation controller's height without adding a toolbar


How to change navigation controller's height without adding a toolbar ? Here is an example of apple's iBooks app that I want to build. I've a solution but isn't perfect( adding a toolbar below the nav controller but it is very ugly)

ibook Nav Controller is perfect

I added a toolbar below the nav controller


Solution

  • I think this is what you want, screenshot

    You can not change navbar height,but you can put a view under it,and use autolayout and shadow to make it looks like part of navbar.Set it to the class you made

    1. Write a view to act as the extendbar

      class ExtendNavView:UIView{
          override func willMoveToWindow(newWindow: UIWindow?) {
             let scale = UIScreen.mainScreen().scale
             self.layer.shadowOffset = CGSizeMake(0, 1.0/scale)
      
             self.layer.shadowRadius = 0;
             self.layer.shadowColor = UIColor.blackColor().CGColor
             self.layer.shadowOpacity = 0.25
           }
       }
      
    2. Drag a UIView and put it under the navBar,then set autolayout to make it always under the nav enter image description here

    3. Change the navBar property in your viewController

      class ViewController: UIViewController {
          override func viewDidLoad() {
              self.navigationController?.navigationBar.translucent = false
              self.navigationController?.navigationBar.shadowImage = UIImage(named: "TransparentPixel")
           self.navigationController?.navigationBar.setBackgroundImage(UIImage(named:"Pixel"), forBarMetrics:UIBarMetrics.Default)
      
            }
        }
      

    The two image used here(Note:they are Translucent)

    Pixel

    url= "https://i.sstatic.net/gFwyN.png"

    TransparentPixel

    url = "https://i.sstatic.net/zpQw4.png "

    You can also look at the Apple example project,you can also find the two images there https://developer.apple.com/library/ios/samplecode/NavBar/Introduction/Intro.html