0

Here what i done for custom navigation controller.i was added this code inside of viewDidLoad method.

    import UIKit

class Login: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        navigatonBar()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func navigatonBar(){

        let codedLabel:UILabel = UILabel()
        codedLabel.frame = CGRect(x: 0, y:-45, width: self.view.frame.width, height: 200)
        codedLabel.textAlignment = .center
        codedLabel.text = "Login"
        codedLabel.textColor = .white
        codedLabel.font=UIFont.systemFont(ofSize: 22)
        let navigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width,height: 75))

        navigationBar.backgroundColor = UIColor.red
        navigationBar.isTranslucent = true
        navigationBar.barTintColor = .red
        self.view.addSubview(navigationBar)
        self.view.addSubview(codedLabel)



    }


}

enter image description here

but i am getting a Black line in the navigation

Faizul Karim
  • 333
  • 2
  • 13

2 Answers2

1

It is hard to determine the exact issue since you haven't posted all of your code, but my guess is that you have a UINavigationController with a custom view controller as the root view controller of the UINavigationController. If this is the case, I believe your issue is that you're adding a second navigation bar as a subview of your custom view controller's view. Don't do that. Remove the code below:

let codedLabel:UILabel = UILabel()
codedLabel.frame = CGRect(x: 0, y:-45, width: self.view.frame.width, height: 200)
codedLabel.textAlignment = .center
codedLabel.text = "Login"
codedLabel.textColor = .white
codedLabel.font=UIFont.systemFont(ofSize: 22)
let navigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width,height: 75))

navigationBar.backgroundColor = UIColor.red
navigationBar.isTranslucent = true
navigationBar.barTintColor = .red
self.view.addSubview(navigationBar)
self.view.addSubview(codedLabel)

and customize the UINavigationController's UINavigationBar:

self.title = "Login"
if let navigationBar = self.navigationController?.navigationBar {
    navigationBar.backgroundColor = UIColor.red
    navigationBar.isTranslucent = true
    navigationBar.barTintColor = .red
    navigationBar.titleTextAttributes = [NSAttributedStringKey.font:  UIFont.systemFont(ofSize: 22), NSAttributedStringKey.foregroundColor: UIColor.white]
}
Jake
  • 13,097
  • 9
  • 44
  • 73
0

im pretty sure that is the shadowImage, to remove it just add this navigationBar.shadowImage = UIImage(), doesn't work if you set it to nil (nil is the default value). Edit: I missed the translucent point, set translucent to false, if don't the nav bar will add a UIVisualEffect on the navBar code : navigationBar.isTranslucent = false

Luis Perez
  • 111
  • 8