作者热门文章
- objective-c - iOS 5 : Can you override UIAppearance customisations in specific classes?
- iphone - 如何将 CGFontRef 转换为 UIFont?
- ios - 以编程方式关闭标记的信息窗口 google maps iOS
- ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"
我尝试将 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/
我是一名优秀的程序员,十分优秀!