呈现的UIView中的高度可变

时间:2019-03-27 17:14:35

标签: swift uiview scrollview

0

我有一个分段控制器,可将两个UIViews xib调用到屏幕上。每个视图的长度因内容而异。由于每个视图的内容是不同的。 UIView是在滚动视图中设置的。

当使用分段控制器选择选定的UIView时,我将其拉到其他UIView的前面。问题是当向下滚动时,较长的视图仍然可见。我不知道如何只允许滚动向下滚动到拉到最前面的UIView的底部。

我在tabselected函数中正在使用simpleViewX.isHidden或simpleViewY.isHidden,但这并不能真正解决问题,因为我仍然可以向下滚动到空白区域。

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
}
}
}

1 个答案:

答案 0 :(得分:0)

您可以尝试

func scrollViewDidScroll(_ scrollView: UIScrollView) {

    let selectedView = firstView.isHidden ? secondView : firstView

    if scrollView.contentOffset.y > selectedView.frame.height {

        scrollView.setContentOffset(selectedView.frame.height, animated: false)
    }

}

如果需要,还可以添加一些填充