使用分段控件更改视图
Change views using Segmented Control
我需要使用分段控件更改视图。在下面的示例中,我将两个视图容器放在同一位置:
第二个容器是隐藏的,我每次使用分段控件都会通过代码显示出来。 (虽然它也没有显示。)
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
})
}
}
}
你知道还有其他方法吗?
有什么方法可以像 TAB 一样自定义 SegmentedControl 吗?
如果你能给我一个项目就太好了。但如果您可以尝试执行后续步骤,也许不需要它。
我会用不同的方式来做。在您的 VC 中添加 UI 视图。称之为容器视图。然后在 xib (nib) more here and here 中创建 2 个单独的视图。然后将这两个视图添加到容器视图中:
view.addSubsire(view1)
和 view.addSubsire(view2)
然后就像您所做的那样检查分段控件并显示您需要的视图。
不要忘记 UI 调试。使用这个惊人的功能:link
实施起来可能需要一些时间,但这些是您在任何情况下都需要了解的基础知识,因此肯定会有用。
希望对您有所帮助!祝你好运!
PS。上面发布的答案 - 不确定它是否有帮助,因为您需要标签为半页?
这里我已经根据您的要求创建了一个完整的解决方案。
Swift 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)
}
}
故事板截图
我需要使用分段控件更改视图。在下面的示例中,我将两个视图容器放在同一位置:
第二个容器是隐藏的,我每次使用分段控件都会通过代码显示出来。 (虽然它也没有显示。)
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
})
}
}
}
你知道还有其他方法吗?
有什么方法可以像 TAB 一样自定义 SegmentedControl 吗?
如果你能给我一个项目就太好了。但如果您可以尝试执行后续步骤,也许不需要它。
我会用不同的方式来做。在您的 VC 中添加 UI 视图。称之为容器视图。然后在 xib (nib) more here and here 中创建 2 个单独的视图。然后将这两个视图添加到容器视图中:
view.addSubsire(view1)
和view.addSubsire(view2)
然后就像您所做的那样检查分段控件并显示您需要的视图。
不要忘记 UI 调试。使用这个惊人的功能:link
实施起来可能需要一些时间,但这些是您在任何情况下都需要了解的基础知识,因此肯定会有用。
希望对您有所帮助!祝你好运!
PS。上面发布的答案 - 不确定它是否有帮助,因为您需要标签为半页?
这里我已经根据您的要求创建了一个完整的解决方案。
Swift 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)
}
}
故事板截图