2016-02-22 53 views
1

我正在创建一个小型聊天应用程序,其中使用UIScrollView的自定义子类来显示消息。这个程序只是为了练习,所以我不想使用第三方库。我通过Apple阅读technical note 2154和几个教程来解释这个,我正在实现这个UIScrollView通过自动布局,我的实现几乎可以工作,但我的UIScrollView的内容视图似乎并没有填满所有可用的空间。UIScrollView动态高度内容(自动布局)不能填满整个空间

呈现了滚动的代码是:

public class ChatView: UIScrollView { 
    private var contentView: UIView 

    ... 

    // This get called by all the init methods. contentView is already created (contentView = UIView()) 
    private func setupViews() { 
     self.translatesAutoresizingMaskIntoConstraints = false 
     contentView.translatesAutoresizingMaskIntoConstraints = false 

     self.addSubview(contentView) 

     let constraint1 = NSLayoutConstraint(item: self, attribute: .Top, relatedBy: .Equal, toItem: contentView, attribute: .Top, multiplier: 1.0, constant: 0.0) 
     let constraint2 = NSLayoutConstraint(item: self, attribute: .Bottom, relatedBy: .Equal, toItem: contentView, attribute: .Bottom, multiplier: 1.0, constant: 0.0) 
     let constraint3 = NSLayoutConstraint(item: self, attribute: .Width, relatedBy: .Equal, toItem: contentView, attribute: .Width, multiplier: 1.0, constant: 0.0) 
     let constraint4 = NSLayoutConstraint(item: self, attribute: .CenterX, relatedBy: .Equal, toItem: contentView, attribute: .CenterX, multiplier: 1.0, constant: 0.0) 

     self.addConstraints([constraint1, constraint2, constraint3, constraint4]) 

     self.layoutIfNeeded() 

     // Later, the messages are added to the contentView. I don't think is relevant to see the exact code (but I can post it if needed) 
     // Each message is added using autolayout and the constraints only reference the messages themselves and contentView 
    } 
} 

当我添加一个ChatView到(使用故事板)我的视图控制器,其四边固定到观点这不是他的层次,下面的问题发生的情况:

Screenshot of the problem

在图像中,所述滚动视图不能向上滚动任何更多。似乎有一个空间应该填补,而不是。如果向下滚动,我会遇到完全相同的问题,但空白空间低于内容。在下面的图片,你可以看到,内容查看比ChatView本身最小:

The debugger view of the problem wo constraints

而且同一视图层次结构,但与所示的限制:

The debugger view of the problem with constraints

两个图像中在后台查看是ChatView,所选的是contentView。我一直无法确定内容视图不包含完整的ChatView空间的原因。

在此先感谢!

回答

1

我终于在another stackoverflow question搜索不同的问题时偶然发现了答案。关键是打开故事板并将容器视图控制器“AdjustsScrollViewInsets”设置为“NO”。

在代码它只是(视图控制器内):

self.automaticallyAdjustsScrollViewInsets = NO; 
相关问题