gpt4 book ai didi

ios - 如何在 UINavigationBar 中添加 UISegmentedControl?

转载 作者:技术小花猫 更新时间:2023-10-29 10:18:55 32 4
gpt4 key购买 nike

我尝试将 UISegmentedControl 添加到带有标题的 UINavigationBar 底部。但是我不能添加它,我不能在 tableView.headerView 中添加 UISegmentedControl,因为我需要在 tableView.headerView 中添加搜索栏,所以只有一种解决方案,我需要将 UISegmentedControl 添加到 UINavigationBar 的底部。有人有其他想法可以解决这种情况吗?

这是我尝试过的代码(使用 Swift):

        class searchingViewController: UITableViewController{ 
override func viewDidLoad() {
var items: [AnyObject] = ["Searching Method A", "Searching Method B"]
var searchSC:UISegmentedControl!
searchSC.frame = CGRectMake(0, 0, frame.width - 20, 30)
searchSC.selectedSegmentIndex = 1
searchSC.backgroundColor = UIColor(white: 1, alpha: 1)
searchSC.layer.cornerRadius = 5.0
navigateUIToolBar = UIToolbar(frame: CGRectMake(frame.minX + 10, ios7_8Info.getStatusBarHeight()+self.navigationController!.navigationBar.frame.height+frame.minY+(21/2),
frame.width - 20, 30))

navigateUIToolBar.addSubview(searchSC)
self.navigationController?.navigationBar.addSubview(navigateUIToolBar)
}
}

最佳答案

在 Swift 5 的 UINavigationBar 中添加段控件

    let segment: UISegmentedControl = UISegmentedControl(items: ["First", "Second"])
segment.sizeToFit()
if #available(iOS 13.0, *) {
segment.selectedSegmentTintColor = UIColor.red
} else {
segment.tintColor = UIColor.red
}
segment.selectedSegmentIndex = 0
segment.setTitleTextAttributes([NSAttributedString.Key.font : UIFont(name: "ProximaNova-Light", size: 15)!], for: .normal)
self.navigationItem.titleView = segment

关于ios - 如何在 UINavigationBar 中添加 UISegmentedControl?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30504503/

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