Search code examples
swiftswift3uinavigationcontrolleruinavigationbar

Navigation Bar - Padding after relaunching app


I am setting a custom height for the navigation bar, also the status bar is hidden:

hidden status bar

override func viewDidLayoutSubviews() {
    let bounds = self.navigationController!.navigationBar.bounds
    self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: bounds.width, height: 60)
}
override var prefersStatusBarHidden: Bool {
    return true
}

I also tried:

override func viewDidAppear(_ animated: Bool) {
    self.navigationController!.navigationBar.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 40)

}

but after relaunching app there is a kind of padding on the top:

enter image description here

I saw this answer:

override func viewWillAppear(_ animated: Bool) {
    let bounds = self.navigationController!.navigationBar.bounds
    navigationController?.isNavigationBarHidden = true
    let navBar: UINavigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: bounds.width, height: 60))
    self.view.addSubview(navBar);
    let navItem = UINavigationItem(title: "Add");
    let doneItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: nil, action: #selector(addNewRecipe(_:)));
    navItem.rightBarButtonItem = doneItem;
    navBar.setItems([navItem], animated: false);
    view.addSubview(navBar)
}

Is there another way than hiding the navigation bar and writing each navigation items programmatically?

Thanks!


Solution

  • Details

    xCode 8.3, Swift 3.1

    Full sample

    Classes

    import UIKit
    
    class NavigationController: UINavigationController {
    
        override func viewDidLayoutSubviews() {
            navigationBar.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 100)
        }
    }
    
    class ViewController: UIViewController {
    
        override var prefersStatusBarHidden: Bool {
            return true
        }
    }
    

    Main.storyboard

    <?xml version="1.0" encoding="UTF-8"?>
    <document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12118" systemVersion="16E195" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="NJV-2W-zhz">
        <device id="retina4_7" orientation="portrait">
            <adaptation id="fullscreen"/>
        </device>
        <dependencies>
            <deployment identifier="iOS"/>
            <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12086"/>
            <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
        </dependencies>
        <scenes>
            <!--Main-->
            <scene sceneID="tne-QT-ifu">
                <objects>
                    <viewController id="BYZ-38-t0r" customClass="ViewController" customModule="stackoverflow_43295886" customModuleProvider="target" sceneMemberID="viewController">
                        <layoutGuides>
                            <viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
                            <viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
                        </layoutGuides>
                        <view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
                            <rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
                            <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                            <color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                        </view>
                        <navigationItem key="navigationItem" title="Main" id="CEB-N4-YPg"/>
                    </viewController>
                    <placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
                </objects>
                <point key="canvasLocation" x="897" y="791"/>
            </scene>
            <!--Navigation Controller-->
            <scene sceneID="rqG-Xx-QcO">
                <objects>
                    <navigationController automaticallyAdjustsScrollViewInsets="NO" id="NJV-2W-zhz" customClass="NavigationController" customModule="stackoverflow_43295886" customModuleProvider="target" sceneMemberID="viewController">
                        <toolbarItems/>
                        <navigationBar key="navigationBar" contentMode="scaleToFill" id="klc-WP-4es">
                            <rect key="frame" x="0.0" y="0.0" width="375" height="44"/>
                            <autoresizingMask key="autoresizingMask"/>
                        </navigationBar>
                        <nil name="viewControllers"/>
                        <connections>
                            <segue destination="BYZ-38-t0r" kind="relationship" relationship="rootViewController" id="vdV-Sr-o02"/>
                        </connections>
                    </navigationController>
                    <placeholder placeholderIdentifier="IBFirstResponder" id="Dng-hb-kZ9" userLabel="First Responder" sceneMemberID="firstResponder"/>
                </objects>
                <point key="canvasLocation" x="133.59999999999999" y="791.15442278860576"/>
            </scene>
        </scenes>
    </document>
    

    enter image description here

    Result

    enter image description here