-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathViewController05_NavigationBarDisable.swift
82 lines (60 loc) · 3.03 KB
/
ViewController05_NavigationBarDisable.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//
// ViewController05_NavigationBarDisable.swift
// NXNavigationExtensionDemo
//
// Created by lidan on 2021/11/8.
//
import UIKit
class ViewController05_NavigationBarDisable: CustomTableViewController {
private lazy var tableHeaderView = UIView(frame: .zero)
private lazy var segmentedControl: UISegmentedControl = {
let segmentedControl = UISegmentedControl(items: ["Tap1", "Tap2"])
segmentedControl.selectedSegmentIndex = 0
segmentedControl.translatesAutoresizingMaskIntoConstraints = false
segmentedControl.addTarget(self, action: #selector(changeSegmentedControl(_:)), for: .valueChanged)
return segmentedControl
}()
private var leftConstraint: NSLayoutConstraint?
private var rightConstraint: NSLayoutConstraint?
private var heightConstraint: NSLayoutConstraint?
@objc
private func changeSegmentedControl(_ segmentedControl: UISegmentedControl) {
print("Value changed: \(segmentedControl.titleForSegment(at: segmentedControl.selectedSegmentIndex) ?? "")")
}
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = nil
tableView.contentInsetAdjustmentBehavior = .never
tableHeaderView.addSubview(segmentedControl)
segmentedControl.bottomAnchor.constraint(equalTo: tableHeaderView.bottomAnchor, constant: -4).isActive = true
leftConstraint = segmentedControl.leadingAnchor.constraint(equalTo: tableHeaderView.leadingAnchor)
leftConstraint?.isActive = true
rightConstraint = segmentedControl.trailingAnchor.constraint(equalTo: tableHeaderView.trailingAnchor)
rightConstraint?.priority = .defaultHigh
rightConstraint?.isActive = true
heightConstraint = segmentedControl.heightAnchor.constraint(equalToConstant: 0)
heightConstraint?.isActive = true
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
guard let navigationBarFrame = navigationController?.navigationBar.frame else { return }
let tableHeaderViewX = navigationBarFrame.minX
let tableHeaderViewY = navigationBarFrame.minY
let tableHeaderViewWidth = navigationBarFrame.width
let tableHeaderViewHeight = navigationBarFrame.maxY
tableHeaderView.frame = CGRect(x: tableHeaderViewX, y: tableHeaderViewY, width: tableHeaderViewWidth, height: tableHeaderViewHeight)
tableView.tableHeaderView = tableHeaderView
guard let safeAreaInsets = navigationController?.navigationBar.safeAreaInsets else { return }
heightConstraint?.constant = navigationBarFrame.height * 0.8
leftConstraint?.constant = safeAreaInsets.left
rightConstraint?.constant = -safeAreaInsets.right
}
}
extension ViewController05_NavigationBarDisable {
override var nx_translucentNavigationBar: Bool {
return true
}
override var nx_enableFullScreenInteractivePopGesture: Bool {
return true
}
}