更改 UINavigationBar 相对于 titleView 的高度 - iOS 11+

问题描述

我正在尝试实现一个棘手的 UINavigationBar,它是一个普通的,带有标题UISearchController 上下,除了标题搜索之间应该有一个自定义 SegmentedControl吧。

我不使用情节提要并尝试创建一个自定义视图以设置为 titleViewUINavigationItem,但是约束不断被打破。我进行了研究,发现在 iOS 11+ 中这种可能性已经消失。

这是我的代码

class UserPostsController: TableViewController {
    
    private lazy var navigationTitleView: UsersPostsNavigationView = {
        let view = UsersPostsNavigationView()
        view.backgroundColor = .orangeyRed
        return view
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationItem.titleView = navigationTitleView
        //navigationItem.searchController = UISearchController()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
         self.navigationController?.navigationBar.frame = CGRect(x: 0,y: 0,width: self.view.frame.width,height: 80)
    }
    
}

class UsersPostsNavigationView: UIView {
    
    private lazy var usersPostsLabelSegment: LabelSegment = {
        let segment = LabelSegment(text: "My Posts",numberOfLines: 1,normalBackgroundColor: .richBlueTwo,normalFont: .systemFont(ofSize: 13,weight: .semibold),normalTextColor: .white,selectedBackgroundColor: .white,selectedFont: .systemFont(ofSize: 13,selectedTextColor: .marineBlue,accessibilityIdentifier: nil)
        return segment
    }()
    
    private lazy var savedPostsLabelSegment: LabelSegment = {
        let segment = LabelSegment(text: "Saved",accessibilityIdentifier: nil)
        return segment
    }()
    
    private lazy var segmentedControl: SegmentedControl = {
        let segmentedControl = SegmentedControl()
        segmentedControl.translatesAutoresizingMaskIntoConstraints = false
        segmentedControl.segments.append(usersPostsLabelSegment)
        segmentedControl.segments.append(savedPostsLabelSegment)
        return segmentedControl
    }()
    
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        label.font = .systemFont(ofSize: 17,weight: .semibold)
        label.text = "My Posts"
        label.textColor = .white
        return label
    }()
    
    override var intrinsicContentSize: CGSize {
        return UIView.layoutFittingExpandedSize
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        translatesAutoresizingMaskIntoConstraints = true
        
        addSubviews(segmentedControl,titleLabel)
        
        titleLabel.topAnchor.constraint(equalTo: topAnchor,constant: 14).isActive = true
        titleLabel.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
        
        segmentedControl.leadingAnchor.constraint(equalTo: leadingAnchor,constant: 16).isActive = true
        segmentedControl.trailingAnchor.constraint(equalTo: trailingAnchor,constant: -16).isActive = true
        segmentedControl.heightAnchor.constraint(equalToConstant: 36).isActive = true
        segmentedControl.topAnchor.constraint(equalTo: titleLabel.bottomAnchor,constant: 16).isActive = true
        segmentedControl.bottomAnchor.constraint(equalTo: bottomAnchor,constant: -4).isActive = true
        
    }
    
    required init?(coder: NSCoder) {
        fatalError()
    }
    
}

我一直在想一种方法来实现这样的事情,不幸的是我找不到一种干净且可维护的方法来做到这一点。

我对任何类型的想法都持开放态度,例如自定义 UINavigationControllerUINavigationBarUINavigationItem 的扩展,其行为类似于 searchController

快速编辑:我知道将 scopeVariablessearchController 设置为 this。但它对我不起作用,因为 SegmentedControl 应该始终可见,tableViewDataSourcesegmentedControl 直接相关,没有任何搜索参数,我需要使用自定义设计。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...