【问题标题】:UIScrollView is not scrolling till the end of the child UIStackViewUIScrollView 不会滚动到子 UIStackView 的末尾
【发布时间】:2017-04-23 05:38:35
【问题描述】:

我有一个 UIScrollView,它有一个 UIStackView 作为它的孩子。

我想将它的滚动链接到内部堆栈视图的大小,以便它能够滚动到它的底部。

我尝试了许多其他方法,例如

scrollView.contentSize = CGSize(width: stackViewMain.frame.width, height: stackViewMain.frame.height)
scrollView.isScrollEnabled = true

scrollView.contentSize = CGSize(width: stackViewMain.frame.width, height: 1000)

我什至尝试在情节提要中硬编码scrollView 的高度。但它仍然滚动了非常少的量,而且不会超过这个量。

我已经为两个视图添加了边缘约束

我该怎么办?

【问题讨论】:

    标签: ios swift uiscrollview storyboard uistackview


    【解决方案1】:

    您应该将堆栈视图的四个边缘约束到滚动视图的四个边缘。 Autolayout 根据滚动视图与其后代之间的约束设置滚动视图的contentSize

    更多信息:Technical Note TN2154: UIScrollView And Autolayout

    【讨论】:

    • 约束已经到位。我已经编辑了问题。
    【解决方案2】:

    @Saad Qureshi,你可以试试ScrollableStackViewhttps://github.com/gurhub/ScrollableStackView

    示例代码 (Swift)

    import ScrollableStackView
    
    var scrollable = ScrollableStackView(frame: view.frame)
    view.addSubview(scrollable)
    
    // add your views with 
    let rectangle = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 55))
    rectangle.backgroundColor = UIColor.blue
    scrollable.stackView.addArrangedSubview(rectangle)
    // ...
    

    示例代码(Objective-C)

    @import ScrollableStackView
    
    ScrollableStackView *scrollable = [[ScrollableStackView alloc] initWithFrame:self.view.frame];
    scrollable.stackView.distribution = UIStackViewDistributionFillProportionally;
    scrollable.stackView.alignment = UIStackViewAlignmentCenter;
    scrollable.stackView.axis = UILayoutConstraintAxisVertical;
    [self.view addSubview:scrollable];
    
    UIView *rectangle = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 55)];
    [rectangle setBackgroundColor:[UIColor blueColor]];
    
    // add your views with
    [scrollable.stackView addArrangedSubview:rectangle]; 
    // ...
    

    如果有帮助,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-26
      • 2019-07-02
      相关资源
      最近更新 更多