简体   繁体   English

iOS 导航栏自定义 titleView 子视图未正确显示

[英]iOS navigation bar custom titleView subviews are not showing properly

iOS navigation bar custom titleView subviews are not showing properly when the parent viewController pushed from another view controller.当父 viewController 从另一个视图控制器推送时,iOS 导航栏自定义 titleView 子视图无法正确显示。

Custom Title view in root view根视图中的自定义标题视图在此处输入图片说明

Custom Title view in View controller after pushing from the different view controller从不同的视图控制器推送后,视图控制器中的自定义标题视图

在此处输入图片说明

Code I tried for Custom TitleView我为自定义 TitleView 尝试的代码

   let height = CGFloat(500)
    self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 50, width: view.frame.width, height: height)
    self.navigationController?.navigationBar.backgroundColor = .blue
    let navView = UIImageView()
    navView.frame = CGRect(x: 0, y: 0, width: view.frame.width-100, height: height)
    navView.backgroundColor = .red
    navigationItem.titleView = navView

Suview速览

        let oneLabel = UILabel()
        oneLabel.frame = CGRect(x: 50, y: navView.frame.size.height-13, width: 26, height: 26)
        oneLabel.text = "1"
        oneLabel.textColor = .white
        oneLabel.backgroundColor = UIColor(hexString: Constants.greenColor)
        oneLabel.textAlignment = .center
        navView.addSubview(oneLabel)

        oneLabel.clipsToBounds = true
        oneLabel.layer.cornerRadius = 13

标签的总高度是 26,你只从 imageview 的高度减少了 13,它显示了标签的一半.. 使标签的 y 值类似于 y: navView.frame.size.height - 30

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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