ios11 Swift 自定义导航条上移20点解决方法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ios11 Swift 自定义导航条上移20点解决方法相关的知识,希望对你有一定的参考价值。

参考技术A 自定义一个类BMNavigationBar继承UINavigationBar,重写layoutSubviews方法(记得初始化的地方改成自定义的BMNavigationBar)

override func layoutSubviews()

super.layoutSubviews()

if #available(ios11.0, *)

for view in self.subviews

if(view.isKind(of:NSClassFromString("_UIBarBackground")!))

view.frame=self.bounds

else if(view.isKind(of:NSClassFromString("_UINavigationBarContentView")!))

var frame= view.frame

frame.origin.y=20

view.frame= frame

Ios Swift:在自定义标签栏中显示导航栏

【中文标题】Ios Swift:在自定义标签栏中显示导航栏【英文标题】:Ios Swift : Displaying Navigation Bar in custom Tabbar 【发布时间】:2016-05-10 14:24:40 【问题描述】:

我正在尝试使用以下代码以编程方式创建一个自定义标签栏以将其放置在屏幕顶部。但问题是,我无法让标签栏顶部的导航栏返回上一个屏幕。 请帮助我。

override func viewDidLoad()

    super.viewDidLoad()

    let SrnBrdVar = UIStoryboard(name: "Main", bundle: nil)
    let TabBarCtrVar = UITabBarController()
    let TabBarVar = TabBarCtrVar.tabBar
    let ObxSrnVar = SrnBrdVar.instantiateViewControllerWithIdentifier("ObxSrnUid")
    let ObxNavSrnVar = UINavigationController(rootViewController: ObxSrnVar)
    let IbxSrnVar = SrnBrdVar.instantiateViewControllerWithIdentifier("IbxSrnUid")
    let IbxNavSrnVar = UINavigationController(rootViewController: IbxSrnVar)

    UIApplication.sharedApplication().delegate!.window?!.rootViewController = TabBarCtrVar

    ObxSrnVar.tabBarItem = UITabBarItem(
        title: "Outbox",
        image: UIImage(named: "NamItmDftImj"),
        selectedImage: UIImage(named: "NamItmSltImj")?.imageWithRenderingMode(.AlwaysOriginal))
    IbxSrnVar.tabBarItem = UITabBarItem(
        title: "Inbox",
        image: UIImage(named: "NamItmDftImj"),
        selectedImage: UIImage(named: "NamItmSltImj")?.imageWithRenderingMode(.AlwaysOriginal))

    TabBarCtrVar.viewControllers = [ObxSrnVar,IbxSrnVar]

    TabBarVar.tintColor = UIColor.greenColor()
    TabBarVar.frame = CGRectMake(
        0,
        UIApplication.sharedApplication().statusBarFrame.size.height + 64,
        UIScreen.mainScreen().bounds.width,
        50)

【问题讨论】:

您是否尝试以编程方式创建 TabBar 控制器,然后向其添加导航栏(也以编程方式)? 我需要所有 Childview 控制器(收件箱和发件箱)从父自定义标签栏 (abv) 视图控制器继承导航栏。 【参考方案1】:

这对我有用。希望对你也有用

 let myStoryBoard = UIStoryboard(name: "Main", bundle: nil)

    let myTabBarController = UITabBarController()

    let leftController = myStoryBoard.instantiateViewControllerWithIdentifier("left")

    let leftNavController = UINavigationController(rootViewController: leftController)

    let rightController = myStoryBoard.instantiateViewControllerWithIdentifier("right")

    let rightNavController = UINavigationController(rootViewController: rightController)

    UIApplication.sharedApplication().delegate!.window?!.rootViewController = myTabBarController

    myTabBarController.addChildViewController(leftNavController)
    myTabBarController.addChildViewController(rightNavController)



    let leftChild = myStoryBoard.instantiateViewControllerWithIdentifier("leftChild")
    let rightChild = myStoryBoard.instantiateViewControllerWithIdentifier("rightChild")


    leftNavController.addChildViewController(leftChild)
    rightNavController.addChildViewController(rightChild)

    print(myTabBarController.viewControllers?.count)
   let but1 = myTabBarController.viewControllers?[0].tabBarItem
    but1!.title = "Left Page"
    let but2 = myTabBarController.viewControllers?[1].tabBarItem
        but2!.title = "Right Page"


    leftNavController.childViewControllers[0].navigationItem.title = "Left View"
    rightNavController.childViewControllers[0].navigationItem.title = "Right View"

【讨论】:

【参考方案2】:

下面的代码对我有用,可以获取导航栏。但我无法添加后退按钮并使其可以移动到上一个屏幕。 谁能帮我解决这个问题。

    let SrnBrdVar = UIStoryboard(name: "Main", bundle: nil)
    let TabBarCtrVar = UITabBarController()
    let TabBarVar = TabBarCtrVar.tabBar
    UIApplication.sharedApplication().delegate!.window?!.rootViewController = TabBarCtrVar

    let Tab1SrnVar = SrnBrdVar.instantiateViewControllerWithIdentifier("Tab1SrnXid")
    let Tab2SrnVar = SrnBrdVar.instantiateViewControllerWithIdentifier("Tab2SrnXid")

    let Tab1NavCtrVar = UINavigationController(rootViewController: Tab1SrnVar)
    let Tab2NavCtrVar = UINavigationController(rootViewController: Tab2SrnVar)

    Tab1NavCtrVar.childViewControllers[0].navigationItem.title = "Nam1TabSrn"
    Tab2NavCtrVar.childViewControllers[0].navigationItem.title = "Nam2TabSrn"

    Tab1NavCtrVar.tabBarItem = UITabBarItem(
        title: "Nam1Tab",
        image: UIImage(named: "NamItmDftImj"),
        selectedImage: UIImage(named: "NamItmSltImj")?.imageWithRenderingMode(.AlwaysOriginal))
    Tab2NavCtrVar.tabBarItem = UITabBarItem(
        title: "Nam2Tab",
        image: UIImage(named: "NamItmDftImj"),
        selectedImage: UIImage(named: "NamItmSltImj")?.imageWithRenderingMode(.AlwaysOriginal))

    TabBarCtrVar.viewControllers = [Tab1NavCtrVar,Tab2NavCtrVar]

    TabBarVar.tintColor = UIColor.greenColor()
    TabBarVar.frame = CGRectMake(
        0,
        UIApplication.sharedApplication().statusBarFrame.size.height + 40,
        UIScreen.mainScreen().bounds.width,
        50)

【讨论】:

以上是关于ios11 Swift 自定义导航条上移20点解决方法的主要内容,如果未能解决你的问题,请参考以下文章

【iOS】解决键盘收起时页面上移的问题

iOS 自定义导航栏 NavigationBar

iOS 自定义导航栏 NavigationBar

如何在 Swift Xcode 中创建自定义导航栏?

iOS 11.2 自定义导航栏无限循环

如何在 Swift iOS 中自定义导航控制器