Ios 如何在UINavigationBar中添加UISegmentedControl?

Ios 如何在UINavigationBar中添加UISegmentedControl?,ios,uitableview,uinavigationbar,uisegmentedcontrol,Ios,Uitableview,Uinavigationbar,Uisegmentedcontrol,我已尝试将UISegmentedControl添加到带有标题的UINavigationBar底部。但是我不能添加它,也不能在tableView.headerView中添加UISegmentedControl,因为我需要tableView.headerView中的搜索栏,所以我只需要在UINavigationBar的底部添加UISegmentedControl。有谁知道我能解决这个问题吗 以下是我尝试过的代码(使用Swift): 要将它放在导航栏中,它有一个左右和标题视图来显示这些内容。。。使用…

我已尝试将
UISegmentedControl
添加到带有标题的
UINavigationBar
底部。但是我不能添加它,也不能在
tableView.headerView
中添加
UISegmentedControl
,因为我需要
tableView.headerView
中的搜索栏,所以我只需要在
UINavigationBar
的底部添加
UISegmentedControl
。有谁知道我能解决这个问题吗

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


要将它放在导航栏中,它有一个左右和标题视图来显示这些内容。。。使用…访问

self.navigationItem.titleView=mySegmentedControl

供将来参考

self.navigationItem.rightBarButtonItem
self.navigationItem.leftBarButtonItems // for adding an Array of items

将其添加到导航视图下方,但使其处于静态状态。。将其添加到viewController。查看。。。您正在使用UITableViewController吗?如果是这样,可以切换到UIViewController并添加tableView,然后将工具栏视图作为子视图添加到self.view。

您可以自定义navigationItem的标题视图,如下所示:

self.navigationItem.titleView = segmentview

您不应该将子视图添加到导航栏,因为在推送或弹出UIViewcontroller之后,子视图仍然存在于导航栏上。

这是我在目标C中的操作方式(抱歉,我还没有学习Swift):

在UITableViewController(分段视图控制器之一)中:


在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

我想你在找这个

let searchVC = self.storyboard?.instantiateViewController(withIdentifier:"idofcontroller")
let searchController = UISearchController(searchResultsController: searchVC)
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search"
navigationItem.searchController = searchController
definesPresentationContext = true

searchController.searchBar.scopeButtonTitles = ["News", "Photos", "Videos"]
searchController.searchBar.delegate = self

您还可以将segment控件添加到搜索栏,而不是导航项。如果你像我一样,我需要一个大标题+搜索栏+导航项,这在当前的顶级答案中是不可能的

@阿披舍克的答案很接近。您可以执行以下操作:

// Create the search controller
let searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.scopeButtonTitles = ["Option 1", "Option 2"]

// Make sure the scope bar is always showing, even when not actively searching
searchController.searchBar.showsScopeBar = true

// Make sure the search bar is showing, even when scrolling
navigationItem.hidesSearchBarWhenScrolling = false

// Add the search controller to the nav item   
navigationItem.searchController = searchController
definesPresentationContext = true

@Gurjit Singh,带有
.showscopebar=true
的那一行是您问题的解决方案。

您可能正在寻找这一行。您可以添加一个屏幕截图或图来说明您正在尝试的内容吗achieve@VivekMolkar我正在寻找[AddSegmentControl in navigationbar]()的解决方案,Rahul Shirphule给出了这个解决方案。我已经尝试过,但是它会删除导航标题。我已经尝试了您的代码,但它与
表视图重叠。headerView
,如何将表视图移动到段控件下方?是的,我正在使用
UITableViewController
。您的回答“UIViewController并添加一个tableView,然后将您的工具栏视图作为子视图”,这对我来说很有用。我已经在一个
UIViewController
中添加了tableView,它可以编辑tableView的偏移量y。非常感谢。好的,很高兴我能帮上忙。有人也否决了我的答案。。。我猜有人因为无法理解而感到沮丧。@Leon请阅读问题,它在UINavigationbar中明确定义,不在UINavigationbar下,但有时有人在没有任何正当理由的情况下投了反对票。我不想就这一愚蠢的问题与你进一步讨论。考虑到其他4个人对我的评论投了赞成票,我认为我的观点是正确的,完全回答了你的问题。如果你不在乎回答他们提出的问题,那么这个平台可能不适合你。但我在2017年4月10日问“谁投了否决票。请给出理由”,为什么你有这么多空闲时间,而你在一年后给出答案,请不要打扰我这是对你的最后警告。你可以看到14票赞成我的答案,我很高兴其他人可以使用我的答案。我如何使它始终可见,即使在编辑结束后。“searchController.searchBar.showscopebar=true”第一次使它们可见,但在编辑完其make后,再次隐藏:-(plz help)最重要的答案是“以前为我工作”。我不会说它现在不工作或不工作,因为我现在不维护该应用程序,而且它当时正在工作。
    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
let searchVC = self.storyboard?.instantiateViewController(withIdentifier:"idofcontroller")
let searchController = UISearchController(searchResultsController: searchVC)
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search"
navigationItem.searchController = searchController
definesPresentationContext = true

searchController.searchBar.scopeButtonTitles = ["News", "Photos", "Videos"]
searchController.searchBar.delegate = self
// Create the search controller
let searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.scopeButtonTitles = ["Option 1", "Option 2"]

// Make sure the scope bar is always showing, even when not actively searching
searchController.searchBar.showsScopeBar = true

// Make sure the search bar is showing, even when scrolling
navigationItem.hidesSearchBarWhenScrolling = false

// Add the search controller to the nav item   
navigationItem.searchController = searchController
definesPresentationContext = true