【问题标题】:Navigation Bar - Padding after relaunching app导航栏 - 重新启动应用程序后的填充
【发布时间】:2017-04-08 15:23:44
【问题描述】:

我正在为导航栏设置自定义高度,状态栏也被隐藏:

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
}

我也试过了:

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

}

但重新启动应用后,顶部有一种填充:

我看到了这个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)
}

除了隐藏导航栏并以编程方式编写每个导航项之外,还有其他方法吗?

谢谢!

【问题讨论】:

    标签: swift swift3 uinavigationcontroller uinavigationbar


    【解决方案1】:

    详情

    xCode 8.3,斯威夫特 3.1

    完整样本

    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>
    

    结果

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-10
      • 2021-04-12
      • 2022-10-18
      • 1970-01-01
      • 1970-01-01
      • 2014-09-14
      • 2017-11-18
      • 1970-01-01
      相关资源
      最近更新 更多