【问题标题】:overlapping UIView issue using segmented controller使用分段控制器重叠 UIView 问题
【发布时间】:2019-07-29 23:41:39
【问题描述】:

我有一个分段控制器,它在屏幕上调用两个 UIViews xib。由于内容的不同,每个视图的长度都会有所不同。由于每个视图的内容不同。 UIView 设置在滚动视图中。

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

我在 tabselected 函数中玩弄 simpleViewX.isHiddensimpleViewY.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
    }
}
}

【问题讨论】:

    标签: ios swift uiview uiscrollview


    【解决方案1】:

    我假设您在滚动视图中使用shiftView 作为“容器”,并使用加载的simpleViewXsimpleViewY 来确定@​​987654325@ 的高度...

    不要将简单视图添加为shiftView 的子视图,而是使用垂直UIStackView 作为shiftView 的子视图。将您的简单视图添加为堆栈视图的arrangedSubviews。要“切换”哪个视图可见,请隐藏另一个视图。堆栈视图将根据剩余的可见视图自动变短或变高,并且通过将shiftView 的高度限制为堆栈视图的高度,您的可滚动高度也将自动设置。

    如果你这样做,就不需要.bringSubviewToFront

    【讨论】:

    • 感谢您的回复。我选择了我的shiftView 并将其嵌入到stackView 中。我想看看它是否有效 simpleViewX = SimpleVC0().viewUIStackView.init(arrangedSubviews: [simpleViewX!]) 但这没有用。为什么我的shiftView 中没有显示simpleViewX
    • @pete800 - 你误会了。将UIStackView 添加到您的shiftView。将其约束在所有 4 个侧面。然后使用.addArrangedSubview() 将您的simpleViewXsimpleViewY 添加到堆栈视图,而不是shiftView.addSubview()
    • 谢谢,我现在就这么做了。它已经停止了重叠,但每个堆栈视图子视图的长度相同。即使 xib 视图长度不同。
    • @pete800 - 如果您发布“simpleView”的示例,我会给您一个快速示例,说明如何操作。
    • 谢谢。我为此设置了一个新问题,因为这个问题是基于您帮助我解决的重叠问题。这个新问题基于与堆栈长度不匹配的滚动长度。 stackoverflow.com/questions/55399808/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-18
    • 1970-01-01
    • 2023-04-06
    • 2021-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多