垂直滚动不能以编程方式运行UIScrollView

时间:2022-07-29 19:42:04

I am not sure what is wrong with my code. I have embedded UIView inside the UIScrollView. There are some controls which have been embedded inside this UIView. I am expecting the scrolling to happen for the UIView but it does not. Eventually I am unable to view all the controls. I am just pasting my code below. Appreciate if somebody points out what I am missing here.

我不确定我的代码有什么问题。我在UIScrollView中嵌入了UIView。这个UIView中嵌入了一些控件。我期待UIView的滚动会发生,但事实并非如此。最终我无法查看所有控件。我只是粘贴下面的代码。感谢有人指出我在这里缺少的东西。

UIView *parentView = [[UIView alloc]init];
[parentView setTranslatesAutoresizingMaskIntoConstraints:NO];
parentView.backgroundColor = [UIColor whiteColor];

[self.view addSubview: parentView];


NSArray  *parentViewHConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[parentView(==mainView)]" options:0 metrics:0 views:@{@"parentView": parentView, @"mainView":self.view}];
NSArray  *parentViewVConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[parentView]-10-|" options:0 metrics:0 views:@{@"parentView": parentView}];
[self.view addConstraints:parentViewHConstraints];
[self.view addConstraints:parentViewVConstraints];


self.myScrollView = [[UIScrollView alloc] init];
//CGSize scrollableSize = CGSizeMake(100, 200);
//[self.myScrollView setContentSize:scrollableSize];
[parentView addSubview:self.myScrollView];
self.myScrollView.backgroundColor = [UIColor lightGrayColor];
self.myScrollView.translatesAutoresizingMaskIntoConstraints  = NO;


NSArray  *scrollViewHContraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|" options:0 metrics:0 views:@{@"scrollView": self.myScrollView}];
[parentView addConstraints:scrollViewHContraints];


UIView *contentView = [[UIView alloc]init];
contentView.translatesAutoresizingMaskIntoConstraints  = NO;
[self.myScrollView addSubview:contentView];
self.myScrollView.showsVerticalScrollIndicator = YES;
self.myScrollView.pagingEnabled = YES;
self.myScrollView.contentSize = CGSizeMake(510,221);

 NSArray  *contentViewHConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[contentview(==scrollView)]|" options:0 metrics:nil views:@{@"contentview": contentView,@"scrollView": self.myScrollView}];
 NSArray  *contentViewVConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[contentview(==scrollView)]|" options:0 metrics:nil views:@{@"contentview": contentView,@"scrollView": self.myScrollView}];

[self.myScrollView addConstraints:contentViewHConstraints];
[self.myScrollView addConstraints:contentViewVConstraints];





UIFont *headingFont = [UIFont fontWithName:@"Helvetica Neue" size:40];

self.m_ObjTopHeadingLbl = [[UILabel alloc]init];
[self.m_ObjTopHeadingLbl setText:@"Registration"];
self.m_ObjTopHeadingLbl.font = headingFont;
[self.m_ObjTopHeadingLbl setTextColor:[UIColor redColor]];
self.m_ObjTopHeadingLbl.translatesAutoresizingMaskIntoConstraints = NO;
[contentView addSubview:self.m_ObjTopHeadingLbl];


NSArray *topHeadingHorzConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[registView]-|" options:0 metrics:nil views:@{@"registView": self.m_ObjTopHeadingLbl}];
[contentView addConstraints:topHeadingHorzConstraint];

// Name field
self.m_ObjNameField = [[JVFloatLabeledTextField alloc]init];
self.m_ObjNameField.borderStyle = UITextBorderStyleRoundedRect;

self.m_ObjNameField.translatesAutoresizingMaskIntoConstraints = NO;
self.m_ObjNameField.placeholder = [NSString stringWithFormat:@"Enter your short name"];
[contentView addSubview:self.m_ObjNameField];


NSArray *nameFieldHorzConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[nameFieldView]-|" options:0 metrics:nil views:@{@"nameFieldView": self.m_ObjNameField}];
[contentView addConstraints:nameFieldHorzConstraint];


// Info on the name field
UILabel* nameFieldLbl = [[UILabel alloc]init];
[nameFieldLbl setText:@"Will be visible to all on ipomo"];
UIFont *nameFieldLblFont = [UIFont fontWithName:@"Arial-BoldMT" size:13];
[nameFieldLbl setFont:nameFieldLblFont];
[nameFieldLbl setTextColor:[UIColor grayColor]];
nameFieldLbl.translatesAutoresizingMaskIntoConstraints = NO;
[contentView addSubview:nameFieldLbl];

NSArray *nameFieldlblHorzConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[nameFieldLbl]-|" options:0 metrics:nil views:@{@"nameFieldLbl": nameFieldLbl}];
[contentView addConstraints:nameFieldlblHorzConstraint];

// Mobilenumber field
self.m_ObjMobNo = [[JVFloatLabeledTextField alloc]init];
    self.m_ObjMobNo.borderStyle = UITextBorderStyleRoundedRect;
self.m_ObjMobNo.translatesAutoresizingMaskIntoConstraints = NO;
self.m_ObjMobNo.placeholder = [NSString stringWithFormat:@"Enter your mobilenumber"];
[contentView addSubview:self.m_ObjMobNo];



 NSArray *mobnoFieldHorzConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[mobnoField]-|" options:0 metrics:nil views:@{@"mobnoField": self.m_ObjMobNo}];
[contentView addConstraints:mobnoFieldHorzConstraint];

// Info on the mobile number field
UILabel* mobnoFieldLbl = [[UILabel alloc]init];
[mobnoFieldLbl setText:@"To send you the activation code. Hidden and secure"];
dispatch_async(dispatch_get_main_queue(), ^{
    mobnoFieldLbl.preferredMaxLayoutWidth = self.view.bounds.size.width;
});
UIFont *mobnoFieldLblFont = [UIFont fontWithName:@"Arial-BoldMT" size:13];
[mobnoFieldLbl setFont:mobnoFieldLblFont];
mobnoFieldLbl.numberOfLines = 0;
[mobnoFieldLbl setTextColor:[UIColor grayColor]];
mobnoFieldLbl.translatesAutoresizingMaskIntoConstraints = NO;
[contentView addSubview:mobnoFieldLbl];


NSArray *mobnoFieldlblHorzConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[mobnoFieldlbl]-|" options:0 metrics:nil views:@{@"mobnoFieldlbl": mobnoFieldLbl}];
[contentView addConstraints:mobnoFieldlblHorzConstraint];

// Email id field

self.m_ObjEmailId = [[JVFloatLabeledTextField alloc]init];
self.m_ObjEmailId.borderStyle = UITextBorderStyleRoundedRect;
self.m_ObjEmailId.translatesAutoresizingMaskIntoConstraints = NO;
self.m_ObjEmailId.placeholder = [NSString stringWithFormat:@"Enter your email id"];
[contentView addSubview:self.m_ObjEmailId];

  NSArray *emailFieldHorzConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[emailField]-|" options:0 metrics:nil views:@{@"emailField": self.m_ObjEmailId}];
[contentView addConstraints:emailFieldHorzConstraint];

// Info on the email field
UILabel* emailFieldLbl = [[UILabel alloc]init];
[emailFieldLbl setText:@"To send analytics and reports on happenings in you room(s). Hidden and secure"];
emailFieldLbl.numberOfLines = 0;
UIFont *emailFieldLblFont = [UIFont fontWithName:@"Arial-BoldMT" size:13];
dispatch_async(dispatch_get_main_queue(), ^{
    emailFieldLbl.preferredMaxLayoutWidth = self.view.bounds.size.width;
});
[emailFieldLbl setFont:emailFieldLblFont];
[emailFieldLbl setTextColor:[UIColor grayColor]];
emailFieldLbl.translatesAutoresizingMaskIntoConstraints = NO;
[contentView addSubview:emailFieldLbl];

   NSArray *emailFieldlblHorzConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[emailFieldlbl]-|" options:0 metrics:nil views:@{@"emailFieldlbl": emailFieldLbl}];
[contentView addConstraints:emailFieldlblHorzConstraint];

// Promo code field
self.m_ObjPromoCode = [[JVFloatLabeledTextField alloc]init];
self.m_ObjPromoCode.borderStyle = UITextBorderStyleRoundedRect;
self.m_ObjPromoCode.translatesAutoresizingMaskIntoConstraints = NO;
self.m_ObjPromoCode.placeholder = [NSString stringWithFormat:@"Enter promocode (if applicable)"];
[contentView addSubview:self.m_ObjPromoCode];


NSArray *promocodeFieldHorzConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[promocodeField]-|" options:0 metrics:nil views:@{@"promocodeField": self.m_ObjPromoCode}];
[contentView addConstraints:promocodeFieldHorzConstraint];

// Submit button
self.m_ObjSubmitBut = [[UIButton alloc]init];

[self.m_ObjSubmitBut setTitle: [NSString stringWithFormat:@"SUBMIT"] forState:UIControlStateNormal];
self.m_ObjSubmitBut.backgroundColor = [UIColor redColor];
[self.m_ObjSubmitBut setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
self.m_ObjSubmitBut.translatesAutoresizingMaskIntoConstraints = NO;
self.m_ObjSubmitBut.layer.cornerRadius = 10;
self.m_ObjSubmitBut.clipsToBounds = YES;
[parentView addSubview:self.m_ObjSubmitBut];


NSDictionary *myTopViews = @{
                             @"scrollView": self.myScrollView,
                             @"submitButton": self.m_ObjSubmitBut,
                             };

NSArray *myTopVConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]-[submitButton(40)]|" options:0 metrics:nil views:myTopViews];
NSArray *myTopHConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[submitButton]-|" options:0 metrics:nil views:@{@"submitButton": self.m_ObjSubmitBut}];


[parentView addConstraints:myTopVConstraints];
[parentView addConstraints:myTopHConstraints];



NSDictionary* myViews = @{
                          @"registView": self.m_ObjTopHeadingLbl,
                          @"nameView": self.m_ObjNameField,
                          @"nameFieldLbl": nameFieldLbl,
                          @"mobnoView":self.m_ObjMobNo,
                          @"mobnoFieldLbl":mobnoFieldLbl,
                          @"emailView":self.m_ObjEmailId,
                          @"emailFieldLbl":emailFieldLbl,
                          @"promocodeView":self.m_ObjPromoCode
                          };

NSDictionary* myMetrics = @{
                            @"sepHeight" : @30.0f,
                            @"sepHeight1" : @5.0f
                            };
NSArray *otherConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[registView]-sepHeight-[nameView]-sepHeight1-[nameFieldLbl]-sepHeight-[mobnoView]-sepHeight1-[mobnoFieldLbl]-sepHeight-[emailView]-sepHeight1-[emailFieldLbl]-sepHeight-[promocodeView]" options:0 metrics:myMetrics views:myViews];

[contentView addConstraints:otherConstraints];

3 个解决方案

#1


0  

You're setting the size of your contentview to be equal to the size of the containing scrollview. Logically this means the scrollview has no area to scroll to and therefore will be fixed. You need to make the contentview longer (for vertical scrolling) or wider (for horizontal scrolling).

您将contentview的大小设置为等于包含scrollview的大小。逻辑上这意味着scrollview没有可以滚动的区域,因此将被修复。您需要使内容视图更长(对于垂直滚动)或更宽(对于水平滚动)。

#2


0  

I think you are missing to mention scrollEnabled of UIScrollView, try following line and hope it works for you.

我想你很想提到UIScrollView的scrollEnabled,试试下面这行并希望它对你有用。

scrollView.scrollEnabled = YES;  

Also set contentSize and its size should be greater than size of containing window to allow it to be scrollable.

还设置contentSize,其大小应大于包含窗口的大小,以允许它可滚动。

[self.scrollforImage setContentSize:CGSizeMake(3000, 3000)];// replace 3000, 3000 according to your need

#3


0  

The constraints for the scrollview in your code are not sufficient. When using auto-layout, ensure that a proper chain of constraints are added in the vertical and horizontal directions.

代码中scrollview的约束是不够的。使用自动布局时,请确保在垂直和水平方向上添加适当的约束链。

For a scroll view to scroll, these are some general guidelines,

要滚动滚动视图,这些是一些通用指南,

  1. Ensure all the different sections of the UI have sufficient number of vertical and horizontal constraints. Take a simple UILabel in the screen. It needs a x,y position - can be a leading/top, centre attribute and it needs a size definition - either fixed width and height or a constraint with the nearby elements. It needs the position - size combination clearly defined for it to be managed by auto-layout.

    确保UI的所有不同部分都有足够数量的垂直和水平约束。在屏幕上看一个简单的UILabel。它需要一个x,y位置 - 可以是一个前导/顶部,中心属性,它需要一个大小定义 - 固定宽度和高度或附近元素的约束。它需要明确定义的位置 - 大小组合,以便通过自动布局进行管理。

  2. If you set the translatesAutoresizingMaskIntoConstraints to NO, what you are saying is that the component shouldn't take the frame,size already set to be constraint requirements. If you want to explicitly mention frame or size for some elements, leave this as YES.

    如果将translatesAutoresizingMaskIntoConstraints设置为NO,那么您所说的是组件不应该采用框架,已将大小设置为约束要求。如果要明确提及某些元素的框架或大小,请将其保留为YES。

  3. If the contentview is pinned in all four sides to the scrollview and the scrollview has its size,position defined as constraints or frame, it will be enough to make the content scrollable.

    如果内容视图在滚动视图的所有四个边都固定,并且滚动视图的大小,位置定义为约束或框架,则足以使内容可滚动。

  4. If the content size of the contentview changes dynamically during different events, you can set the scrollview.contentsize to the contentview.width and contentview.height explicitly.

    如果contentview的内容大小在不同事件期间动态更改,则可以将scrollview.contentsize显式设置为contentview.width和contentview.height。

Also, check this, a technical note from Apple on AutoLayout and Scrollviews.

另外,请查看Apple,AutoLayout和Scrollviews上的技术说明。

#1


0  

You're setting the size of your contentview to be equal to the size of the containing scrollview. Logically this means the scrollview has no area to scroll to and therefore will be fixed. You need to make the contentview longer (for vertical scrolling) or wider (for horizontal scrolling).

您将contentview的大小设置为等于包含scrollview的大小。逻辑上这意味着scrollview没有可以滚动的区域,因此将被修复。您需要使内容视图更长(对于垂直滚动)或更宽(对于水平滚动)。

#2


0  

I think you are missing to mention scrollEnabled of UIScrollView, try following line and hope it works for you.

我想你很想提到UIScrollView的scrollEnabled,试试下面这行并希望它对你有用。

scrollView.scrollEnabled = YES;  

Also set contentSize and its size should be greater than size of containing window to allow it to be scrollable.

还设置contentSize,其大小应大于包含窗口的大小,以允许它可滚动。

[self.scrollforImage setContentSize:CGSizeMake(3000, 3000)];// replace 3000, 3000 according to your need

#3


0  

The constraints for the scrollview in your code are not sufficient. When using auto-layout, ensure that a proper chain of constraints are added in the vertical and horizontal directions.

代码中scrollview的约束是不够的。使用自动布局时,请确保在垂直和水平方向上添加适当的约束链。

For a scroll view to scroll, these are some general guidelines,

要滚动滚动视图,这些是一些通用指南,

  1. Ensure all the different sections of the UI have sufficient number of vertical and horizontal constraints. Take a simple UILabel in the screen. It needs a x,y position - can be a leading/top, centre attribute and it needs a size definition - either fixed width and height or a constraint with the nearby elements. It needs the position - size combination clearly defined for it to be managed by auto-layout.

    确保UI的所有不同部分都有足够数量的垂直和水平约束。在屏幕上看一个简单的UILabel。它需要一个x,y位置 - 可以是一个前导/顶部,中心属性,它需要一个大小定义 - 固定宽度和高度或附近元素的约束。它需要明确定义的位置 - 大小组合,以便通过自动布局进行管理。

  2. If you set the translatesAutoresizingMaskIntoConstraints to NO, what you are saying is that the component shouldn't take the frame,size already set to be constraint requirements. If you want to explicitly mention frame or size for some elements, leave this as YES.

    如果将translatesAutoresizingMaskIntoConstraints设置为NO,那么您所说的是组件不应该采用框架,已将大小设置为约束要求。如果要明确提及某些元素的框架或大小,请将其保留为YES。

  3. If the contentview is pinned in all four sides to the scrollview and the scrollview has its size,position defined as constraints or frame, it will be enough to make the content scrollable.

    如果内容视图在滚动视图的所有四个边都固定,并且滚动视图的大小,位置定义为约束或框架,则足以使内容可滚动。

  4. If the content size of the contentview changes dynamically during different events, you can set the scrollview.contentsize to the contentview.width and contentview.height explicitly.

    如果contentview的内容大小在不同事件期间动态更改,则可以将scrollview.contentsize显式设置为contentview.width和contentview.height。

Also, check this, a technical note from Apple on AutoLayout and Scrollviews.

另外,请查看Apple,AutoLayout和Scrollviews上的技术说明。