[英]UIScrollView not scrolling when add constrains for UIPageControl in iOS
I have 1 UIPageControl
and 1 UIScrollView
. 我有1个
UIPageControl
和1个UIScrollView
。 When I set constrains for UIPageControl
, my UIScrollView
cannot scroll. 当我为
UIPageControl
设置约束时,我的UIScrollView
无法滚动。 This is my code: 这是我的代码:
//set contentsize for uiscrollview
[self.scrollView setContentSize:CGSizeMake(320, 1440)];
UIPageControl *imagePVControl = self.imagePVControl;
[imagePVControl setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[imagePVControl]-0-|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(imagePVControl)]];
When I use [imagePVControl setTranslatesAutoresizingMaskIntoConstraints:NO];
当我使用
[imagePVControl setTranslatesAutoresizingMaskIntoConstraints:NO];
the scrollview cannot scroll , but if I use [imagePVControl setTranslatesAutoresizingMaskIntoConstraints:YES];
scrollview无法滚动 ,但是如果我使用
[imagePVControl setTranslatesAutoresizingMaskIntoConstraints:YES];
, my scrollview can srcoll . ,我的scrollview可以srcoll 。 Can you please help me to fix this issue.
您能帮我解决此问题吗? Thanks.
谢谢。
Also, my XIB is uncheck "Use Autolayout"" . 另外,我的XIB取消选中“使用自动布局”。
UPDATE UPDATE
I change to my code as below. 我将代码更改如下。 The
UIScrollview
can scroll but the UIPageControl
is aligned in the center not the left. UIScrollview
可以滚动,但UIPageControl
在中心而不是在左侧对齐。 I want to algin the UIPageControl
in the left but it is not successful. 我想在左边添加
UIPageControl
,但它不成功。 Please help me to correct it. 请帮助我纠正它。 Thanks
谢谢
NSDictionary *views = @{@"ScrollImageView":self.scrollViewImage,@"TableView":self.productDetailTable,@"InfoView":self.infoView};
NSDictionary *metrics = @{ @"ScrollImageheight" : ScrollImage_height,@"Tablewidth" : width, @"Tableheight" : Table_height,@"infoViewheight" : InfoView_height,@"infoViewWidth" : InfoView_width};
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[TableView(Tablewidth)]-0-|" options:0 metrics:metrics views:views]];
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[InfoView(infoViewWidth)]-0-|" options:0 metrics:metrics views:views]];
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[ScrollImageView(ScrollImageheight)]-0-[InfoView(infoViewheight)]-0-[TableView(Tableheight)]-|" options:0 metrics:metrics views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[imagePVControl]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(imagePVControl)]];
I am also struggling with constraints in the UIScrollView. 我还在UIScrollView中的约束方面苦苦挣扎。 What I learn recently is that, once you set
TranslatesAutoresizingMaskIntoConstraints
to NO, you will lose a lot of settings. 我最近了解到的是,一旦将
TranslatesAutoresizingMaskIntoConstraints
设置为NO,您将丢失很多设置。 Like contentSize
and the frames of views inside the UIScrollView
, that's weird. 像
contentSize
和UIScrollView
内部的视图框架一样,这很奇怪。 What I learnt is that if you want to add constraint to the views inside a UIScrollView
, you have to set everything of whoseViews. 我了解到的是,如果要向
UIScrollView
内的视图添加约束,则必须设置其whosViews的所有内容。 In your case, I think you should try this way: [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[imagePVControl(320)]-0-|", options:0, metrics:nil, views:NSDictionaryOfVariableBindings(imagePVControl)]]; 对于您的情况,我认为您应该尝试以下方式:[self.view addConstraints:[NSLayoutConstraintconstraintsWithVisualFormat:@“ H:| -10- [imagePVControl(320)]-0- |”,选项:0,指标:无,的观点:NSDictionaryOfVariableBindings(imagePVControl)]]; [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[imagePVControl(1440)]-0-|", options:0, metrics:nil, views:NSDictionaryOfVariableBindings(imagePVControl)]];
[self.view addConstraints:[NSLayoutConstraintconstraintsWithVisualFormat:@“ V:| -10- [imagePVControl(1440)]-0- |”,options:0,metrics:nil,views:NSDictionaryOfVariableBindings(imagePVControl)]]; In these two constraints, I specify the top, bottom, left and right, besides that, I also set the width and the height of the
imagePVControl
The reason here is that, once you set the TranslatesAutoresizingMaskIntoConstraints
to NO, you tell the computer that you are going to tell it how big the contentSize
is, instead of setting the contentSize
, you tell it all the information of the views inside the content. 在这两个约束中,我指定了上,下,左和右,此外,我还设置了
imagePVControl
的宽度和高度。原因是,一旦将TranslatesAutoresizingMaskIntoConstraints
设置为NO,就会告诉计算机您我们将告诉它contentSize
大小,而不是设置contentSize
,而是告诉它内容内部视图的所有信息。 The main difference here is that UIScrollView
does not have a fixed edge around it, so you can not set constraints as other kinds of UIView
. 这里的主要区别是
UIScrollView
周围没有固定的边缘,因此不能像其他类型的UIView
一样设置约束。
You could learn from this: https://developer.apple.com/library/ios/technotes/tn2154/_index.html Hope this help :) 您可以从中学到以下知识: https : //developer.apple.com/library/ios/technotes/tn2154/_index.html希望获得帮助:)
After reviewing this problem, I found the problem. 复查此问题后,我发现了问题。 The way you treat with
UIPageControl
was right, you should add a few more code here. 使用
UIPageControl
的方式是正确的,您应该在此处添加更多代码。 I assume that you have a UIView
called myView in the top of the scrollview, and then you have a UITableView
called myTable below myView. 我假设您在滚动视图的顶部有一个名为myView的
UIView
,然后在myView之下有一个名为myTable的UITableView
。 And you want to set myView 240 height, and myTable 1200 height. 您想要设置myView 240高度和myTable 1200高度。 Then here is the code:
然后是以下代码:
NSDictionary *viewsDic = NSDictionaryOfVariableBindings(myView, myTable);
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[myView(320)]-|", options:0, metrics:nil, views:viewDic]];
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[myTable(320)]-|", options:0, metrics:nil, views:viewDic]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[myView(240)]-[myTable(1200)]-|", options:0, metrics:nil, views:viewDic]];
The main reason is that you set a subview of the scrollView TranslatesAutoresizingMaskIntoConstraints
to NO, this will make the contentSize unable to be manually set. 主要原因是您将scrollView
TranslatesAutoresizingMaskIntoConstraints
的子视图设置为NO,这将导致无法手动设置contentSize。 So I give out the how big the content of scrollView will be, it automatically calculates the contentSize. 因此,我给出了scrollView的内容大小,它会自动计算contentSize。
Why use constraints? 为什么要使用约束? why can't you
setPagingEnabled:YES
on UIScrollView
and then place UIPageControl
on top of your UIScrollView
in the hierarchy? 为什么不能在
UIScrollView
上设置setPagingEnabled:YES
,然后将UIPageControl
放在层次结构中的UIScrollView
顶部?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.