0

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
    }
}
}
kitchen800
  • 197
  • 1
  • 12
  • 36

1 Answers1

1

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.

DonMag
  • 69,424
  • 5
  • 50
  • 86
  • Thanks for the reply. I selected my `shiftView` and embedded it in a `stackView`. I to see if it worked did this `simpleViewX = SimpleVC0().view` along with `UIStackView.init(arrangedSubviews: [simpleViewX!])` But that didn't work. why didn't that show `simpleViewX` in my `shiftView`? – kitchen800 Mar 27 '19 at 14:49
  • @pete800 - you misunderstood. Add a `UIStackView` to your `shiftView`. Constrain it on all 4 sides. Then use `.addArrangedSubview()` to add your `simpleViewX` and `simpleViewY` to the stack view, instead of `shiftView.addSubview()`. – DonMag Mar 27 '19 at 17:26
  • Thanks, I did that now. It has stopped the over lap, each stack view subview is the same length though. even though the xib view lengths are different. – kitchen800 Mar 28 '19 at 10:07
  • @pete800 - if you post an example of your "simpleView" I'll give you a quick example of how to do it. – DonMag Mar 28 '19 at 11:59
  • Thank you. i have set up an new question for this, as this one was based on the overlapping which you have helped me with. this new question is based on the scroll length not matching the stack length. https://stackoverflow.com/questions/55399808/scrollview-with-embedded-stackview-issue – kitchen800 Mar 28 '19 at 14:18