简体   繁体   中英

navigationItem.TitleView not working on iOS 10

I have a problem with a titleview in navigationBar. The thing is that its not displayed when i assign a view into titleView. I've tried to use navigationItem.title = @"eqweqweq"; but nothing happens.

This view involved is created by code, i dont know if that's the problem, because the other ViewControllers where i've used that worked perfectly.

Is there any bug in iOS 10 that i cant use titleView? Sometimes it works sometimes not.

I've search on google but nothing helped me. I hope somebody can help me T_T.

thank you

Just use one at a time.

Either set titleView of navigationItem like below:

UILabel *lblTitle = [[UILabel alloc] init];
lblTitle.text = @"eqweqweq";
lblTitle.backgroundColor = [UIColor clearColor];

[lblTitle sizeToFit];

self.navigationItem.titleView = lblTitle;

OR

Directly set title of navigationItem like below:

navigationItem.title=@"eqweqweq"

Make sure you set size of your custom titleView. For example use titleLabel.sizeToFit() before setting titleView.

let titleLabel = UILabel()
titleLabel.attributedText = NSAttributedString(string: title, attributes: attributes)
titleLabel.sizeToFit()  // Important part
navigationItem.titleView = titleLabel

Finally i've solved the problem!

The problem was this function:

extension UINavigationController{
func applyWhiteEffect(){
        var bounds = self.navigationBar.bounds
        let whiteView = UIView()
        bounds.origin.y = -20
        bounds.size.height = bounds.size.height + 20
        whiteView.frame = bounds
        whiteView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
        whiteView.userInteractionEnabled = false
        whiteView.backgroundColor = UIColor.whiteColor()
        whiteView.tag = 1000
        self.navigationBar.addSubview(whiteView)
        self.navigationBar.backgroundColor = UIColor.clearColor()
        self.navigationBar.sendSubviewToBack(whiteView)
    }
}

This function applied a white view and there was a problem with that and iOS 10, so i've changed to this:

func applyWhiteEffect(){
        var bounds = self.navigationBar.bounds
        let whiteView = UIView()
        bounds.origin.y = -20
        bounds.size.height = 20
        whiteView.frame = bounds
        whiteView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
        whiteView.userInteractionEnabled = false
        whiteView.backgroundColor = UIColor.whiteColor()
        whiteView.tag = 1000
        self.navigationBar.addSubview(whiteView)
        self.navigationBar.backgroundColor = UIColor.whiteColor()
        self.navigationBar.sendSubviewToBack(whiteView)
    }

Changing the view to only cover status bar and self.navigationBar.backgroundColor = UIColor.whiteColor()

Thanks guys for helping me anyway :D

尝试在 viewWillApear 函数中设置 titleView。

Please try this. It works for me.

self.title = "eqweqweq"

Hope it will helps you.Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM