【问题标题】:Vertical scrolling not working UIScrollView programatically垂直滚动无法以编程方式运行 UIScrollView
【发布时间】:2016-04-10 09:39:16
【问题描述】:

我不确定我的代码有什么问题。我在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];

【问题讨论】:

  • //CGSize scrollableSize = CGSizeMake(100, 200);您没有将框架设置为滚动视图,此行已被注释
  • 不,它不起作用。事实上,我在 iphone4s 模拟器上试过这个。
  • 您好,您有解决方案吗?

标签: ios uiscrollview vertical-scrolling


【解决方案1】:

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

【讨论】:

  • 做了这个,但没有区别。 NSLayoutConstraint *heightConstraints = [NSLayoutConstraint constraintWithItem:contentView 属性:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.myScrollView 属性:NSLayoutAttributeHeight 乘数:5.0 常数:0];
  • 感谢您的意见。事实上,你的原因是滚动没有发生的根本原因。除了其他约束之外,我只需要添加高度约束才能启用滚动。
【解决方案2】:

我认为您缺少提及UIScrollViewscrollEnabled,请尝试以下行并希望它对您有用。

scrollView.scrollEnabled = YES;  

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

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

【讨论】:

  • 不,它没有。行为是相同的。顺便说一句,将 200 乘以 200 作为内容大小有什么具体原因吗?
  • 不,我只是随机放了 200 乘 200,这取决于你需要做什么。
  • 阿米尔还有什么其他建议吗?
  • 你设置了吗? scrollView.scrollEnabled = YES;
【解决方案3】:

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

对于滚动视图的滚动,这些是一些一般准则,

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

  2. 如果您将 translatesAutoresizingMaskIntoConstraints 设置为 NO,您的意思是组件不应该采用框架,尺寸已经设置为约束要求。如果您想明确提及某些元素的框架或大小,请将其保留为“是”。

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

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

另外,请查看this,这是 Apple 关于 AutoLayout 和 Scrollviews 的技术说明。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-15
    • 2019-11-13
    • 2015-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多