使用SegmentController让TableView消失,UIContainerView出现
Use SegmentController to Make TableView Disappear and UIContainerView Appear
我正在尝试使用段控制器在我的 tableView 和容器视图之间切换,但是当我尝试在它们之间切换时它只成功了一半。 TableView 出现又消失,但容器视图从未出现。
这是我的代码:
@IBAction func switchAction(_ sender: UISegmentedControl) {
if sender.selectedSegmentIndex == 0 {
profileTableView.isHidden = false
modelsContainerView.isHidden = true
} else {
profileTableView.isHidden = true
modelsContainerView.isHidden = false
}
}
更新
如果我使用此代码,则模拟工作正常。容器视图出现,但它不像 tableview 那样填满屏幕。
@IBAction func switchAction(_ sender: UISegmentedControl) {
if sender.selectedSegmentIndex == 0 {
UIView.animate(withDuration: 0.5, animations: {
self.profileTableView.alpha = 1
self.modelsContainerView.alpha = 0
})
} else {
UIView.animate(withDuration: 0.5, animations: {
self.profileTableView.alpha = 0
self.modelsContainerView.alpha = 1
})
}
}
我可以看出它不起作用,因为我已将容器视图的背景颜色设置为粉红色。这就是当我尝试从 TableView(有效)切换到容器 View 时的样子:
所有插座似乎都已连接。我的 UI 设置是段控制器后面的绿色视图,下面有一个 tableView 和一个位于同一位置的 containerView。
非常感谢您的帮助。
试试这个方法...
Seg Background 视图的高度为 45 磅,并且固定顶部、前导、尾随都等于 0
。
Profile Container 的前导、尾随、底部都固定为 0
,顶部固定到 Seg Background 的底部。
但是您看不到 Profile Container(红色背景),因为 Models Container(橙色背景)在它上面,并且...
模型容器宽度和高度相等,水平和垂直居中,全部到配置文件容器。
配置文件容器中嵌入了配置文件 Table VC。
模型容器中嵌入了模型 VC。
想法是:
选择Seg 0
时,配置文件容器为alpha 1, not 隐藏,而模型容器为alpha 0, is ] 隐藏。
When Seg 1
is selected, Profile Container is alpha 0 and is hidden, while Models Container is alpha 1 and not 隐藏。
class SegContainerViewController: UIViewController {
@IBOutlet weak var profileContainerView: UIView!
@IBOutlet weak var modelsContainerView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// start with Profile visible
// so hide Models and set its alphs to 0
self.modelsContainerView.alpha = 0
self.modelsContainerView.isHidden = true
}
@IBAction func switchAction(_ sender: UISegmentedControl) {
// on segment select, the "other" container will be
// transparent and hidden, so
// un-hide it, then animate the alpha for both (for cross-fade)
// on animation completion, hide the now transparent container
if sender.selectedSegmentIndex == 0 {
self.profileContainerView.isHidden = false
UIView.animate(withDuration: 0.5, animations: {
self.profileContainerView.alpha = 1
self.modelsContainerView.alpha = 0
}, completion: { (finished: Bool) in
self.modelsContainerView.isHidden = true
})
} else {
self.modelsContainerView.isHidden = false
UIView.animate(withDuration: 0.5, animations: {
self.profileContainerView.alpha = 0
self.modelsContainerView.alpha = 1
}, completion: { (finished: Bool) in
self.profileContainerView.isHidden = true
})
}
}
}
编辑:
要访问嵌入式视图控制器,请覆盖 prepareForSegue:
var theProfileVC: ProfileTableViewController?
var theModelsVC: ModelsViewControler?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? ProfileTableViewController {
// do something here if desired, like setting a property of the VC
// save a reference so we can use it later
theProfileVC = vc
}
if let vc = segue.destination as? ModelsViewControler {
// do something here if desired, like setting a property of the VC
// save a reference so we can use it later
theModelsVC = vc
}
}
我还用这个示例更新了 GitHub 存储库。
我把它作为一个示例项目,如果你想深入研究它:https://github.com/DonMag/SegmentsAndContainers
我正在尝试使用段控制器在我的 tableView 和容器视图之间切换,但是当我尝试在它们之间切换时它只成功了一半。 TableView 出现又消失,但容器视图从未出现。
这是我的代码:
@IBAction func switchAction(_ sender: UISegmentedControl) {
if sender.selectedSegmentIndex == 0 {
profileTableView.isHidden = false
modelsContainerView.isHidden = true
} else {
profileTableView.isHidden = true
modelsContainerView.isHidden = false
}
}
更新
如果我使用此代码,则模拟工作正常。容器视图出现,但它不像 tableview 那样填满屏幕。
@IBAction func switchAction(_ sender: UISegmentedControl) {
if sender.selectedSegmentIndex == 0 {
UIView.animate(withDuration: 0.5, animations: {
self.profileTableView.alpha = 1
self.modelsContainerView.alpha = 0
})
} else {
UIView.animate(withDuration: 0.5, animations: {
self.profileTableView.alpha = 0
self.modelsContainerView.alpha = 1
})
}
}
我可以看出它不起作用,因为我已将容器视图的背景颜色设置为粉红色。这就是当我尝试从 TableView(有效)切换到容器 View 时的样子:
所有插座似乎都已连接。我的 UI 设置是段控制器后面的绿色视图,下面有一个 tableView 和一个位于同一位置的 containerView。
非常感谢您的帮助。
试试这个方法...
Seg Background 视图的高度为 45 磅,并且固定顶部、前导、尾随都等于 0
。
Profile Container 的前导、尾随、底部都固定为 0
,顶部固定到 Seg Background 的底部。
但是您看不到 Profile Container(红色背景),因为 Models Container(橙色背景)在它上面,并且...
模型容器宽度和高度相等,水平和垂直居中,全部到配置文件容器。
配置文件容器中嵌入了配置文件 Table VC。
模型容器中嵌入了模型 VC。
想法是:
选择Seg 0
时,配置文件容器为alpha 1, not 隐藏,而模型容器为alpha 0, is ] 隐藏。
When Seg 1
is selected, Profile Container is alpha 0 and is hidden, while Models Container is alpha 1 and not 隐藏。
class SegContainerViewController: UIViewController {
@IBOutlet weak var profileContainerView: UIView!
@IBOutlet weak var modelsContainerView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// start with Profile visible
// so hide Models and set its alphs to 0
self.modelsContainerView.alpha = 0
self.modelsContainerView.isHidden = true
}
@IBAction func switchAction(_ sender: UISegmentedControl) {
// on segment select, the "other" container will be
// transparent and hidden, so
// un-hide it, then animate the alpha for both (for cross-fade)
// on animation completion, hide the now transparent container
if sender.selectedSegmentIndex == 0 {
self.profileContainerView.isHidden = false
UIView.animate(withDuration: 0.5, animations: {
self.profileContainerView.alpha = 1
self.modelsContainerView.alpha = 0
}, completion: { (finished: Bool) in
self.modelsContainerView.isHidden = true
})
} else {
self.modelsContainerView.isHidden = false
UIView.animate(withDuration: 0.5, animations: {
self.profileContainerView.alpha = 0
self.modelsContainerView.alpha = 1
}, completion: { (finished: Bool) in
self.profileContainerView.isHidden = true
})
}
}
}
编辑:
要访问嵌入式视图控制器,请覆盖 prepareForSegue:
var theProfileVC: ProfileTableViewController?
var theModelsVC: ModelsViewControler?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? ProfileTableViewController {
// do something here if desired, like setting a property of the VC
// save a reference so we can use it later
theProfileVC = vc
}
if let vc = segue.destination as? ModelsViewControler {
// do something here if desired, like setting a property of the VC
// save a reference so we can use it later
theModelsVC = vc
}
}
我还用这个示例更新了 GitHub 存储库。
我把它作为一个示例项目,如果你想深入研究它:https://github.com/DonMag/SegmentsAndContainers