【问题标题】:Custom titleView changes location after push and pop new screen in stack自定义titleView在堆栈中推送和弹出新屏幕后更改位置
【发布时间】:2017-09-27 21:24:10
【问题描述】:

我正在尝试为带有“标题”和“描述”标签的导航控制器实现自定义 titleView。如果我把这个 titleView 放在第一个 VC 上,它看起来不错。如果我将第二个 VC 推入导航堆栈并弹出它,titleView 的位置就会改变。

titleView 的约束:

titleLabel.translatesAutoresizingMaskIntoConstraints = false
descriptionLabel.translatesAutoresizingMaskIntoConstraints = false
titleLabel.widthAnchor.constraint(equalTo: widthAnchor).isActive = true
descriptionLabel.widthAnchor.constraint(equalTo: titleLabel.widthAnchor).isActive = true
titleLabel.topAnchor.constraint(equalTo: topAnchor).isActive = true
descriptionLabel.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 2.0).isActive = true

在 VC 的 viewDidLoad 中,我使用以下代码插入 titleView

navigationItem.titleView = BarTitleView()
navigationItem.titleView?.bounds = CGRect(x: 0, y: 0, width: view.bounds.width, height: 44)
navigationItem.titleView?.updateConstraints()

我尝试在viewWillAppear 中插入后续行(第二个 VC 有不同的条形按钮,这可能是问题的根源),但没有任何改变

navigationItem.titleView?.bounds = CGRect(x: 0, y: 0, width: view.bounds.width, height: 44)
navigationItem.titleView?.updateConstraints()

我该如何解决这个问题?

【问题讨论】:

    标签: ios swift uikit


    【解决方案1】:

    我在导航栏中创建了标题和副标题,无需界面生成器和约束修改。

    我创建标题视图的函数如下所示:

    class func setTitle(title:String, subtitle:String) -> UIView {
        let titleLabel = UILabel(frame: CGRect(x: 0, y: -8, width: 0, height: 0))
    
        titleLabel.backgroundColor = UIColor.clear
        titleLabel.textColor = UIColor.white
        titleLabel.font = UIFont.boldSystemFont(ofSize: 17)
        titleLabel.text = title
        titleLabel.sizeToFit()
    
        let subtitleLabel = UILabel(frame: CGRect(x: 0, y: 12, width: 0, height: 0))
        subtitleLabel.backgroundColor = UIColor.clear
        subtitleLabel.textColor = UIColor.white
        subtitleLabel.font = UIFont.systemFont(ofSize: 12)
        subtitleLabel.text = subtitle
        subtitleLabel.sizeToFit()
    
        // Fix incorrect width bug
        if (subtitleLabel.frame.size.width > titleLabel.frame.size.width) {
            var titleFrame = titleLabel.frame
            titleFrame.size.width = subtitleLabel.frame.size.width
            titleLabel.frame = titleFrame
            titleLabel.textAlignment = .center
        }
    
        let titleView = UIView(frame: CGRect(x: 0, y: 0, width: titleLabel.frame.size.width, height: titleLabel.frame.size.height))
        titleView.addSubview(titleLabel)
        titleView.addSubview(subtitleLabel)
    
        let widthDiff = subtitleLabel.frame.size.width - titleLabel.frame.size.width
    
        if widthDiff < 0 {
            let newX = widthDiff / 2
            subtitleLabel.frame.origin.x = abs(newX)
        } else {
            let newX = widthDiff / 2
            titleLabel.frame.origin.x = newX
        }
    
        return titleView
    }
    

    然后要在任何视图控制器中使用标题视图,我只需调用此行:

    self.navigationItem.titleView = Functions.Views.setTitle(title: "Title String", subtitle: "Subtitle String")
    

    【讨论】:

    • 当我推到 newVC 然后再返回时,titleview 改变位置到左边,我不知道为什么
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    • 2014-06-20
    • 2014-12-19
    • 1970-01-01
    • 2016-04-04
    • 2020-04-02
    • 1970-01-01
    相关资源
    最近更新 更多