0

Можно ли добавить UISegmentedControl под UINavigationbar в UITableviewController как на скриншоте? Ключевая особенность использовать UITableviewController

8
  • Почему именно UITableviewController?
    – VAndrJ
    30 мар 2017 в 14:25
  • @VAndrJ Чтобы не переделывать текущую UITableviewController в UIViewController. Используя UIViewController знаю как реализовать. 30 мар 2017 в 14:32
  • Добавьте в Container view Ваш UITableviewController и ничего не нужно переделывать.
    – VAndrJ
    30 мар 2017 в 14:57
  • @VAndrJ Вариант конечно, но интересно всё же можно ли модифицировать TableViewController 31 мар 2017 в 7:04
  • Почему просто не сделать table view controller меньшим, и в ету пустоту дабавить UISegmentedControl. 1 апр 2017 в 18:51

1 ответ 1

0

Я нашла решение в этой ветке на Objective-C: https://stackoverflow.com/questions/30504503/how-to-add-the-uisegmentedcontrol-in-uinavigationbar

Переписала на Swift:

override func viewDidLoad() {
    super.viewDidLoad()
    let segmentTitles = ["All", "Not on This Iphone"]
    let segmentedControl = UISegmentedControl(items: segmentTitles)
    segmentedControl.addTarget(self, action: #selector(segmentedClicked), for: UIControlEvents.valueChanged)
    segmentedControl.selectedSegmentIndex = 0
    if let navController = self.navigationController, let tableViewFrame = self.tableView?.frame {
        let topOffset = navController.navigationBar.frame.size.height + UIApplication.shared.statusBarFrame.size.height

        let toolBar = UIToolbar(frame: CGRect(x: 0, y: topOffset, width: navController.navigationBar.frame.size.width, height: 80))
        toolBar.clipsToBounds = true
        let flexibleItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace  , target: nil, action: nil)

        let segmentedControlItem = UIBarButtonItem(customView: segmentedControl)
        toolBar.setItems([flexibleItem,segmentedControlItem,flexibleItem], animated: true)
        self.navigationController?.view.addSubview(toolBar)

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

        self.tableView?.frame(forAlignmentRect: CGRect(x: tableViewFrame.origin.x, y: tableViewFrame.origin.y, width: tableViewFrame.size.width, height: tableViewFrame.size.height + UIApplication.shared.statusBarFrame.size.height))
    }

}
func segmentedClicked(sender:UISegmentedControl) {
    print("set=\(sender.selectedSegmentIndex)")
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.