如何解决更改 UINavigationBar 相对于 titleView 的高度 - iOS 11+
我正在尝试实现一个棘手的 UINavigationBar
,它是一个普通的,带有标题和 UISearchController
上下,除了标题和搜索之间应该有一个自定义 SegmentedControl
吧。
我不使用情节提要并尝试创建一个自定义视图以设置为 titleView
的 UINavigationItem
,但是约束不断被打破。我进行了研究,发现在 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()
}
}
我一直在想一种方法来实现这样的事情,不幸的是我找不到一种干净且可维护的方法来做到这一点。
我对任何类型的想法都持开放态度,例如自定义 UINavigationController
、UINavigationBar
或 UINavigationItem
的扩展,其行为类似于 searchController
快速编辑:我知道将 scopeVariables
的 searchController
设置为 this。但它对我不起作用,因为 SegmentedControl
应该始终可见,tableViewDataSource
与 segmentedControl
直接相关,没有任何搜索参数,我需要使用自定义设计。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。