Swift内容标签?

时间:2018-05-29 20:53:45

标签: ios swift

我正在构建一个应用程序,如果你单击第一个选项卡,它需要有3个选项卡,它是一个tableview,第二个选项卡是一个不同的tableview,第三个选项卡,TextView,这里是我想要做的图像:

enter image description here

每当我谷歌寻找这样的例子时,我都会被提到UITabBarController,我不认为标签栏是我正在寻找的。

2 个答案:

答案 0 :(得分:3)

您正在寻找的是分段控制(UISegmentedControl)。 您可以在本地操作中看到它,例如在Apple iTunes和Health App中。

您可以使用Interface Builder将其连接到UITabBarController,而不是使用UIViewController对不同视图执行segues,而不是

@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 doc @ https://developer.apple.com/documentation/uikit/uisegmentedcontrol?changes=_3

答案 1 :(得分:0)

我会以编程方式创建此布局。你想要一个自定义的外观,所以我不认为标准控件是你最好的选择。

这是一个让你开始实现这个目标的游乐场:

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()

您可以将其复制并粘贴到空的操场上,以查看它的实际效果。基本上,这是根据选择的按钮显示或隐藏适当的视图。如果您愿意,可以替换视图控制器的视图。