gpt4 book ai didi

Swift:导航栏中带有小 TitleView 的 SegmentedControl

转载 作者:行者123 更新时间:2023-11-30 11:35:12 25 4
gpt4 key购买 nike

我试图在我的导航栏上包含一个分段控件,如下所示: enter image description here

这里的想法是文本“fetching...”是一个小的 titleView。但是,我当前的实现将导致文本“正在获取...”在下侧,如下所示: enter image description here

我实现了大标题,以便我可以在导航栏上获得两“行”,否则“正在获取...”一词将隐藏在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

}

有人有什么建议吗?

最佳答案

您可以创建一个 customView 来保存您想要在导航栏中显示的所有 View ,并将该 View 设置为 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

这应该会给你以下结果。您可以使用这些值来做您想做的事情。

enter image description here

关于Swift:导航栏中带有小 TitleView 的 SegmentedControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49841919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com