I have a segmented controller that call two UIViews xibs onto the screen. Each view will vary in length due to their content. As the content for each view is different. The UIView are set in a scroll view.
I am pulling the Selected UIView in front of the others when it is selected using the segmented controller. The problem is that the longer views are still visible when I scroll down. I can't figure out how to only let the scroll go down to the bottom of the UIView that has been pulled to the front.
I was playing around with simpleViewX.isHidden
or simpleViewY.isHidden
in the tabselected function but that doesn't really solve the situation because I can still scroll down into empty space.
class foodinfo: UIViewController {
var counter = Int()
@IBOutlet var tabs: UISegmentedControl!
@IBOutlet var shiftView: UIView!
@IBOutlet var theTitleLable: UILabel!
var simpleViewX: UIView!
var simpleViewY: UIView!
var theTitleArray = ["Title1","Title2","Title3","Title4","Title5","Title6","Title7"]
override func viewDidLoad() {
//Different subViews for each selection
if counter == 0 {
simpleViewX = SimpleVC0().view
simpleViewY = SimpleVC1().view
shiftView.addSubview(simpleViewY)
shiftView.addSubview(simpleViewX)
}
if counter == 1 {
simpleViewX = SimpleVC2().view
simpleViewY = SimpleVC3().view
shiftView.addSubview(simpleViewY)
shiftView.addSubview(simpleViewX)
}
if counter == 2 {
simpleViewX = SimpleVC4().view
simpleViewY = SimpleVC5().view
shiftView.addSubview(simpleViewY)
shiftView.addSubview(simpleViewX)
}
if counter == 3 {
simpleViewX = SimpleVC6().view
simpleViewY = SimpleVC7().view
shiftView.addSubview(simpleViewY)
shiftView.addSubview(simpleViewX)
}
if counter == 4 {
simpleViewX = SimpleVC8().view
simpleViewY = SimpleVC9().view
shiftView.addSubview(simpleViewY)
shiftView.addSubview(simpleViewX)
}
if counter == 5 {
simpleViewX = SimpleVC10().view
simpleViewY = SimpleVC11().view
shiftView.addSubview(simpleViewY)
shiftView.addSubview(simpleViewX)
}
if counter == 6 {
simpleViewX = SimpleVC12().view
simpleViewY = SimpleVC13().view
shiftView.addSubview(simpleViewY)
shiftView.addSubview(simpleViewX)
}
}
func getTitle() {
theTitleLable.text = theTitleArray[counter]
}
@IBAction func tabselected(_ sender: Any) {
switch (sender as AnyObject).selectedSegmentIndex {
case 0:
shiftView.bringSubviewToFront(simpleViewX)
break
case 1:
shiftView.bringSubviewToFront(simpleViewY)
break
default:
break
}
}
}
I'm assuming you are using shiftView
as a "container" inside your scroll view, and using the loaded simpleViewX
and simpleViewY
to determine the height of shiftView
...
Instead of adding the simple views as subviews of shiftView
, use a vertical UIStackView
as the subview of shiftView
. Add your simple views as arrangedSubviews
of the stack view. To "toggle" which view is visible, hide the other view. The stack view will automatically get shorter or taller based on the remaining visible view, and, by constraining the height of shiftView
to the height of the stack view, your scrollable height will also be automatically set.
No need for .bringSubviewToFront
if you do it that way.