Search code examples
iosuiscrollviewvertical-scrolling

Vertical scrolling not working UIScrollView programatically


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.

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];

Solution

  • 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).