如何从自定义导航栏中删除此黑线

时间:2018-02-14 22:56:41

标签: ios swift

这里我为自定义导航controller.i所做的就是在viewDidLoad方法中添加了这段代码。

    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

但我在导航中得到一条黑线

2 个答案:

答案 0 :(得分:1)

由于您尚未发布所有代码,因此很难确定确切的问题,但我的猜测是您有UINavigationController个自定义视图控制器作为{的根视图控制器{1}}。如果是这种情况,我相信您的问题是您要添加第二个导航栏作为自定义视图控制器视图的子视图。不要这样做。删除下面的代码:

UINavigationController

并自定义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) ' UINavigationController

UINavigationBar

答案 1 :(得分:0)

我很确定这是shadowImage,要删除它只需添加此navigationBar.shadowImage = UIImage(),如果将其设置为nil(nil是默认值)则不起作用。编辑:我错过了半透明点,将半透明设置为false,如果导航栏没有在navBar代码上添加UIVisualEffect:navigationBar.isTranslucent = false