我在uiscrollview中的某些视图上设置边距有问题,我通过IB创建了一个scrollview和3个视图(它们在self.view之外),在我的应用程序中我添加了这个视图:
[scrollview addsubview:view1],eccc
之后我以这种方式添加约束:
[_ingredienti setTranslatesAutoresizingMaskIntoConstraints:NO];
[_scrollView addSubview:_ingredienti];
[_scrollView addConstraint:[NSLayoutConstraint constraintWithItem:_ingredienti
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:_scrollView
attribute:NSLayoutAttributeHeight
multiplier:1
constant:0]];
[_scrollView addConstraint:[NSLayoutConstraint constraintWithItem:_ingredienti
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:_scrollView
attribute:NSLayoutAttributeWidth
multiplier:1
constant:0]];
[_scrollView addConstraint:[NSLayoutConstraint constraintWithItem:_ingredienti
attribute:NSLayoutAttributeLeftMargin
relatedBy:NSLayoutRelationEqual
toItem:_scrollView
attribute:NSLayoutAttributeLeft
multiplier:1
constant:self.scrollView.frame.size.width * 2]];
这对于所有3个视图,显然NSLayoutAttributeLeftMargin有3个不同的值,0,self.scrollView.frame.size.width和2 * self.scrollView.frame.size.width,问题是边距是错误的,是一些像素比self.scrollView.frame.size.width少,哪里出错了?
答案 0 :(得分:0)
似乎没有约束将_ingredienti
顶部,底部或右侧位置链接到_scrollview
,它需要调整其内容视图的大小。请参阅"Pure Autolayout" subsection of Technical Note TN2154: UIScrollView And Autolayout