【问题标题】:Resize content view in UIScrollView no working在 UIScrollView 中调整内容视图的大小不起作用
【发布时间】:2015-04-21 13:24:46
【问题描述】:

我有这个 ViewController :

主视图
_-滚动
__- 内容视图

我需要动态调整内容视图的大小

我有这个方法resizeScrollView(),这是我的方法:

private func resizeScrollView(){
     self.mViewContainer.frame.size.height = 800
    self.mScrollView.contentSize = self.mViewContainer.frame.size
    NSLog("Scroll Height : " + NSNumber(float: Float(self.mScrollView.contentSize.height)).stringValue)
    NSLog("Container Height : " + NSNumber(float: Float(self.mViewContainer.frame.height)).stringValue)

}

我有这些日志:

2015-04-21 15:11:44.854 quotle[3673:60b] Scroll Height : 800
2015-04-21 15:11:44.856 quotle[3673:60b] Container Height : 800

在这一步,两个视图都已正确调整大小。

在我的滚动事件中,我链接了这个方法:

func scrollViewDidScroll(scrollView: UIScrollView) {
    NSLog("Scroll Height : " + NSNumber(float: Float(self.mScrollView.contentSize.height)).stringValue)
    NSLog("Container Height : " + NSNumber(float: Float(self.mViewContainer.frame.height)).stringValue)
}

当我滚动时,我会看到这些日志:

2015-04-21 15:11:43.556 quotle[3673:60b] Scroll Height : 800
2015-04-21 15:11:43.559 quotle[3673:60b] Container Height : 416

所以,当我开始滚动时,我的内容视图会调整大小...

我的问题是:我必须做些什么才能在滚动时将内容视图的高度保持在 800 并且不自动调整内容视图的大小?

你可以用 Obj-C 来响应,我会适应 swift 的。

【问题讨论】:

  • 为什么不加约束?
  • 由于约束,我的内容视图不适合所有滚动视图

标签: ios objective-c swift uiscrollview


【解决方案1】:

试试这个

- (void) fitForScroll:(UIView*) containerViewInScroll :(UIScrollView*)scrollView
{
    float w=0;
    float h=0;
    for (UIView *v in [containerViewInScroll subviews]) {
        float fw = v.frame.origin.x + v.frame.size.width;
        float fh = v.frame.origin.y + v.frame.size.height;
        w = MAX(fw, w);
        h = MAX(fh, h);
    }
    [containerViewInScroll setFrame:CGRectMake(containerViewInScroll.frame.origin.x,containerViewInScroll.frame.origin.y, w, h + 20)];
    [scrollView setContentSize:containerViewInScroll.frame.size];
}


----------

【讨论】:

  • 在界面生成器中尝试等于和高度约束它对我有用
猜你喜欢
  • 2016-01-06
  • 1970-01-01
  • 1970-01-01
  • 2018-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多