gpt4 book ai didi

swift - 以编程方式在导航栏中嵌入分段控件

转载 作者:搜寻专家 更新时间:2023-11-01 05:56:11 24 4
gpt4 key购买 nike

我试图以编程方式在导航栏中插入分段控件,但在导航栏中加载分段 Controller 时遇到了一些问题。我相信这很简单,但请参阅下面的代码。提前致谢!

var segmentedController: UISegmentedControl!

override func viewDidLoad() {
super.viewDidLoad()

navigationItem.titleView = segmentedController
let items = ["Label A", "Label B"]
segmentedController = UISegmentedControl(items: items)

navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Sign Out", style: .plain, target: self, action: #selector(handleSignOut))
navigationItem.leftBarButtonItem?.tintColor = UIColor.black

}

最佳答案

segmentedController初始化后应该添加到导航栏中!

var segmentedController: UISegmentedControl!

override func viewDidLoad() {
super.viewDidLoad()

let items = ["Label A", "Label B"]
segmentedController = UISegmentedControl(items: items)
navigationItem.titleView = segmentedController

navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Sign Out", style: .plain, target: self, action: #selector(handleSignOut))
navigationItem.leftBarButtonItem?.tintColor = UIColor.black
}

关于swift - 以编程方式在导航栏中嵌入分段控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43381842/

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