[英]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?
在這里,我根據您的要求創建了一個完整的解決方案。
斯威夫特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)
}
}
故事板截圖
如果您可以給我發送一個項目,那就太好了。 但是,如果您可以嘗試執行后續步驟,則可能不需要。
我會以不同的方式來做。 在您的VC中添加UIView。 稱之為容器視圖。 然后在此處和此處更多的xib(nib)中創建2個單獨的視圖。 然后將以下兩個視圖添加到容器視圖中: view.addSubsire(view1)
和view.addSubsire(view2)
然后,就像您所做的一樣,只檢查分段控件並顯示所需的視圖。
不要忘記UI調試。 使用此驚人功能: 鏈接
可能需要一些時間來實現,但是無論如何您都需要了解這些基本知識,因此它絕對有用。
希望能幫助到你! 祝好運!
PS。 上面發布的答案-不確定是否有幫助,因為您需要將標簽設為半頁?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.