【问题标题】:UIScrollView SetContentSize with autolayoutUIScrollView SetContentSize 与自动布局
【发布时间】:2016-04-15 09:29:09
【问题描述】:

我在故事板中使用 UIScrollview 并在其中应用自动布局。在 UIScrollview 内,我插入 contentview 作为 UIScrollview 的自动布局需要一个 contentview。现在,根据我的应用场景,我必须更改 contentView 的高度,所以我以编程方式更改了 contentView 的自动布局。

现在我在以编程方式更改自动布局后得到 contentView 的高度 = 445。下面是我的代码

//----set height of content view----//
CGRect contentViewFrame = contentView.frame;
contentViewFrame.size.height= viewMainContent.frame.origin.y+viewMainContent.frame.size.height + 20 ;
contentView.frame = contentViewFrame;
_contentHtConstraint.constant = contentView.frame.size.height;

根据这个UIScrollview 的高度也是445。但是 UIScrollview 的 contentSize 将是 675

谁能告诉我如何管理 contentSize?

【问题讨论】:

  • 你打电话给[self updateViewConstraints]了吗?
  • @zhubch No..我要在哪里打电话?
  • 在你改变约束后调用它。
  • @zhubch 没用..仍然有同样的问题
  • @zhubch 在滚动视图的 contentSize 中出现问题不在滚动视图的高度 //----设置内容视图的高度----// CGRect contentViewFrame = contentView.frame; contentViewFrame.size.height= viewMainContent.frame.origin.y +viewMainContent.frame.size.height + 20 ; contentView.frame = contentViewFrame; _contentHtConstraint.constant = contentView.frame.size.height; _objScroll.contentSize = CGSizeZero; [自我更新视图约束];

标签: ios objective-c iphone uiscrollview autolayout


【解决方案1】:

为您的内容视图添加高度限制。并为其设置出路。 如果您希望在视图控制器启动时更改内容高度。在 viewWillLayoutSubviews 中设置

- (void) viewWillLayoutSubviews{
        [super viewWillLayoutSubviews];

        //Update your content view height, by updating constant size of height constraint
       self.contentViewHeightConstraint.constant = 1000;//set this value as per your need. 

       // update your view constraint if needed
       [self.view updateConstraintsIfNeeded];

      //Set content size for your scrollview 
      [self.scrollView setContentSize:CGSizeMake(self.scrollView.contentSize.width, self.contentViewHeightConstraint.constant)];

}

【讨论】:

  • 现在从您的解决方案中管理滚动内容大小,但禁用用户交互。我在那里什么都做不了。
  • Nilesh.. 我没有使用默认的滚动视图。这里我使用的是 TPKeyboardAvoidingScrollview
  • 我刚刚在 github 上查看了 TPKeyboardAvoidingScrollView。它是 UIScrollview 的子类。您也可以使用 TPKeyboardAvoidingScrollView 的默认滚动视图属性。检查您的滚动视图或内容视图用户交互是否启用。或者可能是任何其他视图与您的内容视图重叠。检查视图的结构.. 希望这可能对您有所帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-27
  • 1970-01-01
  • 1970-01-01
  • 2021-11-17
  • 1970-01-01
  • 1970-01-01
  • 2013-02-27
相关资源
最近更新 更多