How to reuse header section all scene?

后端 未结 3 835
天涯浪人
天涯浪人 2021-01-29 08:42

I want to reuse header section all view controller scene .header section mean green view and label (\"AMAR LIFE\")

Here is my may 1st

相关标签:
3条回答
  • 2021-01-29 09:22

    Try the following steps to achieve your header visible on all views.

    Step 1: Drag a navigationController into your storyBoard and delete tableView rootViewcontroller comes with the naviagtionController.Connect your NavigationController to your tabBarController.Now,NavigationController available to all your view.

    Note: Your storyBoard layout should be look like a below image..

    Step 2: Implement following code to your FirstTabBarController.

       override func viewDidLoad() {
        super.viewDidLoad()
    
        // Apply transparency to NavigationBar.
        navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
        navigationController?.navigationBar.shadowImage = UIImage()
    
        //Setting up scanView background
        let barView = UIView(frame: CGRect(x:0, y:0, width:view.frame.width,height: (navigationController?.navigationBar.frame.height)! + UIApplication.shared.statusBarFrame.height)) 
        barView.backgroundColor=UIColor.green // set any colour you want..
        barView.layer.borderColor = UIColor.black.cgColor
        barView.layer.borderWidth = 3
        navigationController?.navigationBar.addSubview(barView)
    
       //Setting up labelView
        let label = UILabel()
        label.frame = CGRect(x:25, y:5, width:view.frame.width - 50 , height:((navigationController?.navigationBar.frame.height)! + UIApplication.shared.statusBarFrame.height) - 10)
        label.text = "AMAR LIFE"
        label.textAlignment = .center
        label.textColor = .black
        label.font = UIFont(name: "HelveticaNeue-medium", size: CGFloat(40))
        label.layer.borderColor = UIColor.black.cgColor
        label.layer.borderWidth = 3
        barView.addSubview(label)
    
    }
    

    Output:

    0 讨论(0)
  • 2021-01-29 09:37

    Subclass UITabBarController. Make the green view a subview of the tab bar controller's main view. Whenever the selected view controller changes (or anything else happens), keep bringing the green view to the front. (Even better, make the green view a layer, not a view, and give it a higher z-position so that it automatically floats in front of all subviews.)

    0 讨论(0)
  • 2021-01-29 09:38

    you need to add the following method in every view controller

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)
        self.navigationController?.isNavigationBarHidden = false
        self.navigationController?.navigationBar.setBackgroundImage(#imageLiteral(resourceName: "123"), for: .default)
        self.title = "Hi I am tittle"
    }
    

    Also if you want to avoid code duplication and do more good just create one parent UIview and write this method in it and then inherit it in all the other views. However both solutions work fine. you can change the title and image according to your needs. Thanks

    0 讨论(0)
提交回复
热议问题