菜鸟教程小白 发表于 2022-12-11 16:36:09

iOS 导航项横向模式下额外填充顶部的问题


                                            <p><p>当我的应用在横向时,顶部有额外的填充。
这是我以编程方式创建导航栏时的代码。
在横向模式下删除填充顶部有什么建议吗?</p>

<pre><code>    let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 44)

    navigationBar.backgroundColor = UIColor.redColor()
    navigationBar.delegate = self;

    let navigationItem = UINavigationItem()
    navigationItem.title = &#34;Title&#34;

    let btnLeft = UIButton(frame: CGRectMake(0, 0, 44, 44))
    btnLeft.setImage(UIImage(named: “myImage.png&#34;), forState: .Normal)

    let leftButton =UIBarButtonItem()
    leftButton.customView = btnLeft

    navigationItem.leftBarButtonItem = leftButton

    navigationBar.items =

    self.view.addSubview(navigationBar)
</code></pre>

<p>模拟器快照(横向)</p>

<p> <a href="/image/bd0oB.jpg" rel="noreferrer noopener nofollow"><img src="/image/bd0oB.jpg" alt="enter image description here"/></a> </p>

<p>模拟器截图(纵向)</p>

<p> <a href="/image/22GBG.jpg" rel="noreferrer noopener nofollow"><img src="/image/22GBG.jpg" alt="enter image description here"/></a> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您应该使用此设置轻松解决(例如在您的 <code>viewDidLoad</code> 中):</p>

<pre><code>self.edgesForExtendedLayout = UIRectEdge.None
</code></pre>

<p>替代方案:你也可以把它放在你的 <code>UINavigationController</code> 委托(delegate)方法中(如果你在你的类中设置了你的导航委托(delegate) <code>UINavigationControllerDelegate</code> )叫做 <a href="https://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationControllerDelegate_Protocol/#//apple_ref/occ/intfm/UINavigationControllerDelegate/navigationController:willShowViewController:animated:" rel="noreferrer noopener nofollow">willShowViewController</a> :</p>

<pre><code>func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
    viewController.edgesForExtendedLayout = UIRectEdge.None
}
</code></pre>

<p>如果你使用 <code>UITableView</code> 你也可以这样做:</p>

<pre><code> self.automaticallyAdjustsScrollViewInsets = false
</code></pre>

<p>你也可以直接编辑到属性检查器:</p>

<p> <a href="/image/LVp7P.png" rel="noreferrer noopener nofollow"><img src="/image/LVp7P.png" alt="enter image description here"/></a> </p>

<p>如果这些方法中的任何一个都不起作用,默认情况下,您的 <code>UINavigationBar</code> 根据您的方向具有不同的高度。例如,导航栏是纵向 44 点,横向 32 点。
解决方法可以是:</p>

<pre><code>// Create an IBOutlet of your navigationBar height constraint
@IBOutlet weak var navBarHeight: NSLayoutConstraint

override func viewWillLayoutSubviews() {
      super.viewWillLayoutSubviews()
      if self.view.bounds.size.height &gt; self.view.bounds.size.width {
            self.navBarHeight.constant = 44
      } else {
            self.navBarHeight.constant = 32
      }
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于iOS 导航项横向模式下额外填充顶部的问题,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/37487080/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/37487080/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iOS 导航项横向模式下额外填充顶部的问题