首页 文章

设置导航栏 Headers 背景颜色(swift)

提问于
浏览
-1

我想在iOS中更改UI Navbar Headers 的背景 . 我希望最终结果如下图所示:

enter image description here

如果可能,或者我将不得不做一个解决方法,请告诉我 .

Changing navigation bar color in Swift未解决UINavigationBar Headers 文本的背景颜色

3 回答

  • 0

    所以基本上你需要在navigationBarTitleView中添加一个UIView . 然后将标签或UIImageView添加到Uiview,最后将该视图添加到TitleView . 所以我在这里为titleView添加了标签 . 希望这会对你有所帮助: -

    override func viewDidLoad() {
        super.viewDidLoad()
        let navBarView: UIView = UIView()
        navBarView.frame = CGRectMake(0, 0, 100.0, 21.0)
    
        let label: UILabel = UILabel()
        label.frame = CGRectMake(0, 0, 90.0, 21.0)
        label.text = "Your Title"
        label.textAlignment = .Center
        label.backgroundColor = UIColor.grayColor()
        navBarView.addSubview(label)
        self.navigationItem.titleView = navBarView
        // Do any additional setup after loading the view, typically from a nib.
    }
    

    所以这是输出: -

    enter image description here

  • 0

    您最好的选择是使用 UINavigationBartitleView 属性 . 尝试这样的事情

    // Create new label that will service as a title view
        let titleView = UILabel()
        /* customize label to heart's content */
        titleView.text = "TITLE HERE"
        // Set your custom background color
        titleView.backgroundColor = UIColor.grayColor()
        // Now set this is as the titleView of the navigation bar
        self.navigationItem.titleView = titleView
    

    如果您有静态管理的托管 UINavigationBar (不是UINavigationController的一部分) . 这样的事情应该有效:

    navBar.pushNavigationItem(self.navigationItem, animated: NO)
    
  • 0
    class SomeNavigationController: UINavigationController {
    
    override func awakeFromNib() {
        super.awakeFromNib()
    
        self.navigationBar.topItem?.title = NavTitle
        }
    
    }
    

    我从一个常量swift类中引用了 NavTitle . 我希望这有帮助!

相关问题