gpt4 book ai didi

ios - 如何使插入 UINavigationViewController 的 View 正确调整大小?

转载 作者:行者123 更新时间:2023-11-29 02:34:04 34 4
gpt4 key购买 nike

我正在尝试以编程方式创建 View Controller 层次结构(没有 IB 和 Storyboard)。如果我使用普通的 UIViewController,一切都会很好:

// My app delegate
func application(application: UIApplication, didFinishLaunchingWithOptions options: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)

let vc = UIViewController(nibName: nil, bundle: nil)
vc.view = UIView(frame: CGRectZero)
vc.view.backgroundColor = UIColor.whiteColor()
let label = UILabel(frame: CGRectMake(0, 0, 100, 40))
label.text = "Hello!"
vc.view.addSubview(label)
vc.title = "Title"

self.window.rootViewController = vc
self.window.makeKeyAndVisible()
return true
}

然后我尝试将 View Controller 放在导航 Controller 中:

func application(application: UIApplication, didFinishLaunchingWithOptions options: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)

// vc created the same way as above.

let nav = UINavigationController(rootViewController: vc)
self.window.rootViewController = nav

self.window.makeKeyAndVisible()
return true
}

并且标签隐藏在导航栏下方。

很明显,导航 View Controller 没有正确调整 View 的大小。我尝试设置内部 vc 的框架,但这没有效果,可能是因为事情是由自动布局处理的?我应该像在 IB/Storyboard 中那样添加一些约束以使它正常工作,还是应该做一些不同的事情?

最佳答案

您的 View 已正确调整大小,但在 iOS 8 中,由于栏可以是半透明的,因此默认情况下 View 会在其下方延伸。 (这就是您的标签被导航栏隐藏的原因)

据我了解,您想要使用半透明栏,但您不希望内容最终位于它们下方。要实现此目的,您可以编辑 View Controller 的edgesForExtendedLayout 属性。

vc.edgesForExtendedLayout = UIRectEdgeNone

此属性告诉您应在半透明条下延伸哪些边缘(左、右、上、下、全部、无或这些的任意组合),默认为 UIRectEdgeAll。

有关更多信息,请查看documentation还有这个guide .

关于ios - 如何使插入 UINavigationViewController 的 View 正确调整大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26561457/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com