代码之家  ›  专栏  ›  技术社区  ›  user979331

Swift内容标签?

  •  0
  • user979331  · 技术社区  · 6 年前

    我正在构建一个应用程序,如果您单击第一个选项卡,它是一个表视图,第二个选项卡是另一个表视图,第三个选项卡,文本视图,我需要有3个选项卡,下面是我正在尝试执行的操作的图像:

    enter image description here

    每次我在谷歌上搜索类似的例子时,我都会被提到uitabbarcontroller,我不认为标签栏是我要找的。

    2 回复  |  直到 6 年前
        1
  •  3
  •   Jayesh Thanki    6 年前

    你要找的是所谓的分段控制( UISegmentedControl ) 您可以在本地看到它的实际应用,例如在Apple iTunes和健康应用程序中。

    而不是用 UITabBarController ,您可以使用Interface Builder将其连接到 UIViewController 就像这样

    @IBOutlet weak var segmentedControl: UISegmentedControl!
    

    而在

    viewDidLoad() {
        switch segmentedControl.selectedIndex {
             case 0: // Do something on your first picture
                   someFunction()
             case 1: // Do something on your second picture
                   performSegue(withIdentifier: "your identifier here", sender: nil)
             case 2: // Do something on your third picture
                   image.isHidden = true
                   button.isEnabled = false
             default: break
    }
    

    …等等,你也可以启动不同的视图控制器,而不是仅仅操作一个视图,然后你可以通过segues访问它。

    请参阅官方的swift文件@ https://developer.apple.com/documentation/uikit/uisegmentedcontrol?changes=_3

        2
  •  0
  •   Nordeast mohammad_Z74    6 年前

    我将以编程方式创建此布局。您需要自定义外观,因此我认为标准控件不是您的最佳选择。

    这里有一个游乐场,可以让你以一种方式开始这项工作:

    import UIKit
    import PlaygroundSupport
    
    class ViewController: UIViewController {
    
        let view1 = UIView(frame: CGRect(x: 0, y: 100, width: 768, height: 924))
        let view2 = UIView(frame: CGRect(x: 0, y: 100, width: 768, height: 924))
        let view3 = UIView(frame: CGRect(x: 0, y: 100, width: 768, height: 924))
    
        override func viewDidLoad() {
            super.viewDidLoad()
            self.view.frame = CGRect(x: 0, y: 0, width: 768, height: 1024)
            self.view.backgroundColor = .black
    
            // View to have a border around the buttons
            let box = UIView(frame: CGRect(x: 40, y: 20, width: 300, height: 50))
            box.layer.borderColor = UIColor.white.cgColor
            box.layer.borderWidth = 2
            self.view.addSubview(box)
    
            // Tab buttons
            let button1 = UIButton(frame: CGRect(x: 40, y: 20, width: 100, height: 50))
            button1.setTitle("Orange", for: .normal)
            button1.tag = 1
            button1.addTarget(self, action: #selector(tabTouched(sender:)), for: .touchUpInside)
            self.view.addSubview(button1)
    
            let button2 = UIButton(frame: CGRect(x: 140, y: 20, width: 100, height: 50))
            button2.setTitle("Blue", for: .normal)
            button2.tag = 2
            button2.addTarget(self, action: #selector(tabTouched(sender:)), for: .touchUpInside)
            self.view.addSubview(button2)
    
            let button3 = UIButton(frame: CGRect(x: 240, y: 20, width: 100, height: 50))
            button3.setTitle("Green", for: .normal)
            button3.tag = 3
            button3.addTarget(self, action: #selector(tabTouched(sender:)), for: .touchUpInside)
            self.view.addSubview(button3)
    
            // Tab Views
            view1.backgroundColor = .orange
            self.view.addSubview(view1)
    
            view2.backgroundColor = .blue
            view2.alpha = 0
            self.view.addSubview(view2)
    
            view3.backgroundColor = .green
            view3.alpha = 0
            self.view.addSubview(view3)
        }
    
    
        // When each of the buttons are tapped we will hide or show the correct tab's view
        @objc func tabTouched(sender: UIButton) {
            if sender.tag == 1 {
                UIView.animate(withDuration: 0.3) {
                    self.view1.alpha = 1
                    self.view2.alpha = 0
                    self.view3.alpha = 0
                }
            } else if sender.tag == 2 {
                UIView.animate(withDuration: 0.3) {
                    self.view1.alpha = 0
                    self.view2.alpha = 1
                    self.view3.alpha = 0
                }
            } else if sender.tag == 3 {
                UIView.animate(withDuration: 0.3) {
                    self.view1.alpha = 0
                    self.view2.alpha = 0
                    self.view3.alpha = 1
                }
            }
        }
    }
    
    PlaygroundPage.current.needsIndefiniteExecution = true
    PlaygroundPage.current.liveView = ViewController()
    

    您可以复制并粘贴到一个空的操场中,以查看它的运行情况。实际上,这是显示或隐藏基于所选按钮的适当视图。如果需要,可以将视图换成视图控制器。