使用分段控件更改视图

编程入门 行业动态 更新时间:2024-10-27 14:18:47
本文介绍了使用分段控件更改视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要使用分段控件来更改视图.在下面的示例中,我将两个视图容器放在相同的位置:

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

您知道其他方法吗?

是否可以像自定义标签一样自定义SegmentedControl?

Is there any way to customize the SegmentedControl as if it were a TAB?

推荐答案

在这里,我已根据您的要求创建了完整的解决方案.

Here i have created a complete solution as per your requirement.

快捷键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) } }

故事板屏幕截图

更多推荐

使用分段控件更改视图

本文发布于:2023-08-05 17:07:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1306856.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   控件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!