微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决更改 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.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?