当我的应用横向时,顶部有额外的填充。 这是我以编程方式创建导航栏时的代码。 有什么建议可以在横向模式下移除 padding top 吗?
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 = "Title"
let btnLeft = UIButton(frame: CGRectMake(0, 0, 44, 44))
btnLeft.setImage(UIImage(named: “myImage.png"), forState: .Normal)
let leftButton = UIBarButtonItem()
leftButton.customView = btnLeft
navigationItem.leftBarButtonItem = leftButton
navigationBar.items = [navigationItem]
self.view.addSubview(navigationBar)
模拟器快照(横向)
模拟器快照(纵向)
最佳答案
你应该很容易解决这个设置(例如在你的 viewDidLoad
中):
self.edgesForExtendedLayout = UIRectEdge.None
备选方案:您也可以将它放在名为 willShowViewController 的 UINavigationController
委托(delegate)方法中(如果您在类中设置了导航委托(delegate) UINavigationControllerDelegate
) :
func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
viewController.edgesForExtendedLayout = UIRectEdge.None
}
如果您使用 UITableView
,您还可以:
self.automaticallyAdjustsScrollViewInsets = false
您也可以直接编辑属性检查器:
如果这些方法中的任何一个都不起作用,默认情况下您的 UINavigationBar
会根据您的方向有不同的高度。例如,导航栏纵向为 44 点,横向为 32 点。
解决方法可以是:
// Create an IBOutlet of your navigationBar height constraint
@IBOutlet weak var navBarHeight: NSLayoutConstraint
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
if self.view.bounds.size.height > self.view.bounds.size.width {
self.navBarHeight.constant = 44
} else {
self.navBarHeight.constant = 32
}
}
关于横向模式下额外填充顶部的 iOS 导航项目问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37487080/