【发布时间】:2018-07-11 17:38:15
【问题描述】:
我需要使用分段控件更改视图。在以下示例中,我将两个视图容器放置在同一位置:
第二个容器是隐藏的,我每次使用分段控件时都会通过代码显示出来。 (虽然它也没有显示出来。)
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 就好像它是一个 TAB 一样?
【问题讨论】:
-
你只是想隐藏和显示容器。
-
只使用单个 containerView 并根据 segmentControl 索引添加 childViewController 或删除 childViewController
标签: ios swift ios11 swift4 xcode9