简体   繁体   English

使用分段控件更改视图

[英]Change views using Segmented Control

I need to change views using a Segmented Control. 我需要使用分段控件来更改视图。 In the following example I have put two view containers in the same location: 在下面的示例中,我将两个视图容器放在相同的位置:

在此处输入图片说明

The second container is hidden and I will show it through code every time I use the segmented control. 第二个容器是隐藏的,每当我使用分段控件时,我都会通过代码显示它。 (Although it does not show it either.) (尽管它也没有显示。)

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var container1: UIView!
    @IBOutlet weak var container2: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    @IBAction func showComponent(_ sender: Any) {
        if (sender as AnyObject).selectedSegmentIndex == 0 {
            UIView.animate(withDuration: 0.5, animations: {
                self.container1.alpha = 1
                self.container2.alpha = 0
            })
        } else {
            UIView.animate(withDuration: 0.5, animations: {
                self.container1.alpha = 0
                self.container2.alpha = 1
            })
        }
    }

}

Do you know of any other way to do it? 您知道其他方法吗?

Is there any way to customize the SegmentedControl as if it were a TAB? 有什么方法可以像自定义TAB一样自定义SegmentedControl?

Here i have created a complete solution as per your requirement. 在这里,我根据您的要求创建了一个完整的解决方案。

Swift 4 斯威夫特4

//
//  SegementedVC.swift
//
//  Created by Test User on 01/02/18.
//  Copyright © 2018 Test User. All rights reserved.
//

import UIKit

class SegementedVC: UIViewController {

    //----------------------------------------------------------------
    // MARK:-
    // MARK:- Outlets
    //----------------------------------------------------------------

    @IBOutlet weak var segmentControl   : UISegmentedControl!
    @IBOutlet weak var containerView    : UIView!


    //----------------------------------------------------------------
    // MARK:-
    // MARK:- Variables
    //----------------------------------------------------------------

    private lazy var firstViewController: FirstViewController = {
        // Load Storyboard
        let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)

        // Instantiate View Controller
        var viewController = storyboard.instantiateViewController(withIdentifier: "FirstViewController") as! FirstViewController

        // Add View Controller as Child View Controller
        self.add(asChildViewController: viewController)

        return viewController
    }()

    private lazy var secondViewController: SecondViewController = {
        // Load Storyboard
        let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)

        // Instantiate View Controller
        var viewController = storyboard.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController

        // Add View Controller as Child View Controller
        self.add(asChildViewController: viewController)

        return viewController
    }()


    //----------------------------------------------------------------
    // MARK:-
    // MARK:- Abstract Method
    //----------------------------------------------------------------

    static func viewController() -> SegementedVC {
        return UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SegementedView") as! SegementedVC
    }

    //----------------------------------------------------------------
    // MARK:-
    // MARK:- Memory Management Methods
    //----------------------------------------------------------------

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


    //----------------------------------------------------------------
    // MARK:-
    // MARK:- Action Methods
    //----------------------------------------------------------------

    @IBAction func segmentValueChanged(_ sender: UISegmentedControl) {
        updateView()
    }


    //----------------------------------------------------------------
    // MARK:-
    // MARK:- Custom Methods
    //----------------------------------------------------------------

    private func add(asChildViewController viewController: UIViewController) {

        // Add Child View Controller
        addChildViewController(viewController)

        // Add Child View as Subview
        containerView.addSubview(viewController.view)

        // Configure Child View
        viewController.view.frame = containerView.bounds
        viewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]

        // Notify Child View Controller
        viewController.didMove(toParentViewController: self)
    }

    //----------------------------------------------------------------

    private func remove(asChildViewController viewController: UIViewController) {
        // Notify Child View Controller
        viewController.willMove(toParentViewController: nil)

        // Remove Child View From Superview
        viewController.view.removeFromSuperview()

        // Notify Child View Controller
        viewController.removeFromParentViewController()
    }

    //----------------------------------------------------------------

    private func updateView() {
        if segmentControl.selectedSegmentIndex == 0 {
            remove(asChildViewController: secondViewController)
            add(asChildViewController: firstViewController)
        } else {
            remove(asChildViewController: firstViewController)
            add(asChildViewController: secondViewController)
        }
    }

    //----------------------------------------------------------------

    func setupView() {
        updateView()
    }



    //----------------------------------------------------------------
    // MARK:-
    // MARK:- View Life Cycle Methods
    //----------------------------------------------------------------

    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupView()
    }

    //----------------------------------------------------------------

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }

    //----------------------------------------------------------------

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    }
}

Storyboard Screenshots 故事板截图

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

  1. Would be great if you can send me a project. 如果您可以给我发送一个项目,那就太好了。 But maybe it's not needed if you can try to follow next steps. 但是,如果您可以尝试执行后续步骤,则可能不需要。

  2. I would do it a different way. 我会以不同的方式来做。 In your VC add the UIView. 在您的VC中添加UIView。 Call it container view. 称之为容器视图。 Then create 2 separate views in xib (nib) more here and here . 然后在此处此处更多的xib(nib)中创建2个单独的视图。 and then add these 2 views into container view: view.addSubsire(view1) and view.addSubsire(view2) 然后将以下两个视图添加到容器视图中: view.addSubsire(view1)view.addSubsire(view2)

  3. Then as you did just check the segmented control and show the view you need. 然后,就像您所做的一样,只检查分段控件并显示所需的视图。

  4. Don't forget about UI Debugging. 不要忘记UI调试。 Use this amazing feature: link 使用此惊人功能: 链接

It might take some time to implement but these are the basics that you need to know in any case so it definitely would be useful. 可能需要一些时间来实现,但是无论如何您都需要了解这些基本知识,因此它绝对有用。

Hope it helps! 希望能帮助到你! Good luck! 祝好运!

PS. PS。 The answer posted above - not sure if it helps since you need the tabs to be half page? 上面发布的答案-不确定是否有帮助,因为您需要将标签设为半页?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM