我试图在我的navBar上包含一个segmentedControl,如下所示:
这里的想法是文本“fetching ...”是一个小标题。但是,我当前的实现会导致文本“getching ...”在下方,如下所示:
我实现大型游戏,以便在navBar上获得两个“行”,否则“fetching ...”这个词将隐藏在segmentedControl后面。
代码:
let segmentedControl: UISegmentedControl = {
let items = ["Now","In 15 mins", "In 1 hour"]
let sc = UISegmentedControl(items: items)
sc.selectedSegmentIndex = 0
return sc
}()
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.backBarButtonItem?.title = "Back"
navigationItem.largeTitleDisplayMode = .automatic
navigationItem.titleView = segmentedControl
}
有人有任何建议吗?
答案 0 :(得分:0)
您可以创建一个customView
,其中包含您要在views
中显示的所有navigation bar
,并将该视图设置为titleView
,如下所示,
let segmentedControl: UISegmentedControl = {
let items = ["Now","In 15 mins", "In 1 hour"]
let sc = UISegmentedControl(items: items)
sc.selectedSegmentIndex = 0
return sc
}()
let fetchingLabel: UILabel = {
let label = UILabel(frame: .zero)
label.text = "Fetching..."
return label
}()
在viewDidLoad
let customView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 250))
customView.addSubview(segmentedControl)
customView.addSubview(fetchingLabel)
fetchingLabel.frame = CGRect(x: 150, y: 0, width: self.view.frame.width, height: 60)
segmentedControl.frame = CGRect(x: 60, y: 50, width: self.view.frame.width * 0.75, height: 30)
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.largeTitleDisplayMode = .automatic
navigationItem.titleView = customView
这应该给你以下结果。你可以玩这些值来做你想做的事。