【问题标题】:How to add the UISegmentedControl in UINavigationBar?如何在 UINavigationBar 中添加 UISegmentedControl?
【发布时间】:2015-08-10 20:09:33
【问题描述】:

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

这是我尝试过的代码(使用 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)
                  }
          }

【问题讨论】:

标签: ios uitableview uinavigationbar uisegmentedcontrol


【解决方案1】:

在 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

【讨论】:

  • 也有人对我的回答投了反对票……我猜有人因为无法理解而感到沮丧。
  • @Leon 请阅读问题,它清楚地定义了在 UINavigationbar 中而不是在 UINavigationbar 下,但有时有人在没有任何正当理由的情况下投了反对票。
  • 我不想和你进一步讨论这个愚蠢的观点。
  • 考虑到其他 4 人对我的评论表示赞同,我认为我的观点是有效的,并且完美地回答了您的问题。如果您不关心回答被问到的问题,那么这个平台可能不适合您。
  • 但是我在 2017 年 4 月 10 日问“谁投反对票。请给出理由”为什么你有这么多空闲时间并且你在 1 年后给出答案,请不要打扰我这是最后警告你。您可以看到 14 人对我的回答投了赞成票,我很高兴其他人可以使用我的回答。
【解决方案2】:

把它放在导航栏中有一个左右和标题视图,用于这些东西......访问使用......

self.navigationItem.titleView = mySegmentedControl

供以后参考....

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

要将其添加到导航视图下方但使其保持静态...将其添加到 viewController.view...您使用的是 UITableViewController 吗?如果是这样,可能会切换到 UIViewController 并添加一个 tableView,然后您的工具栏视图作为该 self.view 的子视图。

【讨论】:

  • 是的,我正在使用UITableViewController。你的回答“UIViewController 并添加一个 tableView 然后你的工具栏视图作为子视图”这对我有用。我在UIViewController 中添加了tableView,它可以编辑tableview 的偏移量y。非常感谢。
  • 好的,很高兴我能帮上忙。
【解决方案3】:

我想你正在寻找这个。

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

【讨论】:

  • 即使在结束编辑后,我如何才能使其始终可见。 “searchController.searchBar.showsScopeBar = true” 使它们第一次可见,但在结束编辑它的 make 然后再次隐藏。 :-(请帮忙
【解决方案4】:

这就是我在 Objective C 中的做法(抱歉,我还没学过 Swift):

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.viewControllers = [self segmentViewControllers];

    self.segmentedControl = [[UISegmentedControl alloc] initWithItems: [self.segmentedControl addTarget: self
                          action: @selector(segmentClicked:)
                forControlEvents: UIControlEventValueChanged];

    CGFloat topOffset = self.navigationController.navigationBar.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height;

    self.toolbar = [[UIToolbar alloc] initWithFrame: CGRectMake(0, topOffset, self.navigationController.navigationBar.frame.size.width, kDefaultViewHeight)];
    self.toolbar.delegate = self;

    self.navigationController.navigationBar.backgroundColor = [UIColor whiteColor];
    self.toolbar.backgroundColor = [UIColor whiteColor];
    self.toolbar.clipsToBounds = YES;

    UIBarButtonItem *segmentedControlItem = [[UIBarButtonItem alloc] initWithCustomView: self.segmentedControl];
    UIBarButtonItem *flexibleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace
                                                                              target: nil
                                                                              action: nil];

    [self.toolbar setItems: @[flexibleItem, segmentedControlItem, flexibleItem] animated: YES];

    [self.navigationController.view addSubview: self.toolbar];

    self.segmentedControl.selectedSegmentIndex = 0;
}

在 UITableViewController(segmentViewControllers 之一)中:

  self.tableView.contentInset = UIEdgeInsetsMake(topOffset, 0, 0, 0);

    CGRect currentFrame = self.tableView.frame;
    [self.tableView setFrame: CGRectMake(currentFrame.origin.x,
                                     currentFrame.origin.y,
                                     currentFrame.size.width,
                                     currentFrame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height)];

【讨论】:

  • 我试过你的代码,但它与tableView.headerView重叠,我怎样才能将表格视图移动到段控件下方?
【解决方案5】:

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

@Abhishek 的答案很接近。您将执行以下操作:

// 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,.showsScopeBar = true 行是您问题的解决方案。

【讨论】:

  • 最佳答案是以前为我工作。我不会说它现在不工作或不工作,因为我现在不维护该应用程序并且当时它正在工作。
【解决方案6】:

你可以自定义navigationItem的titleView,像这样:

self.navigationItem.titleView = segmentview

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

【讨论】:

  • 我试过了,但是会去掉navigation abr的标题
猜你喜欢
  • 2018-07-26
  • 1970-01-01
  • 2014-07-20
  • 1970-01-01
  • 2011-05-23
  • 2015-09-08
  • 2014-03-20
  • 1970-01-01
相关资源
最近更新 更多