我有这个结构
-Self.View
--CenterView (UIView)
---ScrollView (UIScrollView)
----View (UIView)
-----Contains other views
我也在这个完整的结构中使用AutoLayout,但我的问题是,即使我在构建视图后使用这行代码,滚动也不起作用:
-(void)viewDidLayoutSubviews
{
[scrollAd setContentSize:CGSizeMake(320, 500)];
}
这是我用来从ScrollView开始构建整个结构的代码:
-(UIView*)getAddAdView{
scrollAd = [UIScrollView new];
scrollAd.backgroundColor = [UIColor colorWithRed:213.0/255 green:210.0/255 blue:210.0/255 alpha:1.0];
scrollAd.translatesAutoresizingMaskIntoConstraints = NO;
scrollAd.scrollEnabled=YES;
scrollAd.userInteractionEnabled=YES;
scrollAd.delegate = self;
[viewCenter addSubview:scrollAd];
NSDictionary *viewsDictionary12 = @{@"scrollAd":scrollAd};
NSArray *constraint_H12 = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|-10-[scrollAd]-10-|"] options:0 metrics:nil views:viewsDictionary12];
NSArray *constraint_V12 = [NSLayoutConstraint constraintsWithVisualFormat: [NSString stringWithFormat:@"H:|-10-[scrollAd]-10-|"] options:0 metrics:nil views:viewsDictionary12];
[viewCenter addConstraints:constraint_H12];
[viewCenter addConstraints:constraint_V12];
NSArray *constraint_POS_V12 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[scrollAd]" options:0 metrics:nil views:viewsDictionary12];
NSArray *constraint_POS_H12 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[scrollAd]" options:0 metrics:nil views:viewsDictionary12];
[viewCenter addConstraints:constraint_POS_H12];
[viewCenter addConstraints:constraint_POS_V12];
viewAddAd = [UIView new];
viewAddAd.backgroundColor = [UIColor colorWithRed:213.0/255 green:210.0/255 blue:210.0/255 alpha:1.0];
viewAddAd.translatesAutoresizingMaskIntoConstraints = NO;
[scrollAd addSubview:viewAddAd];
NSDictionary *viewsDictionary = @{@"viewAddAd":viewAddAd, @"scrollAd":scrollAd};
NSArray *constraint_H = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|[viewAddAd(==scrollAd)]|"] options:0 metrics:nil views:viewsDictionary];
NSArray *constraint_V = [NSLayoutConstraint constraintsWithVisualFormat: [NSString stringWithFormat:@"H:|[viewAddAd(==scrollAd)]|"] options:0 metrics:nil views:viewsDictionary];
[scrollAd addConstraints:constraint_H];
[scrollAd addConstraints:constraint_V];
//NSArray *constraint_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[viewAddAd]" options:0 metrics:nil views:viewsDictionary];
//NSArray *constraint_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[viewAddAd]" options:0 metrics:nil views:viewsDictionary];
//[scrollAd addConstraints:constraint_POS_H];
//[scrollAd addConstraints:constraint_POS_V];
UIView *viewSelectCategory = [UIView new];
viewSelectCategory.backgroundColor = [UIColor redColor];
viewSelectCategory.translatesAutoresizingMaskIntoConstraints = NO;
[viewAddAd addSubview:viewSelectCategory];
UIView *viewExpiryDate = [UIView new];
viewExpiryDate.backgroundColor = [UIColor blueColor];
viewExpiryDate.translatesAutoresizingMaskIntoConstraints = NO;
[viewAddAd addSubview:viewExpiryDate];
UIView *viewKeywords = [UIView new];
viewKeywords.backgroundColor = [UIColor blueColor];
viewKeywords.translatesAutoresizingMaskIntoConstraints = NO;
[viewAddAd addSubview:viewKeywords];
NSDictionary *viewsDictionary2 = @{@"viewSelectCategory":viewSelectCategory, @"viewExpiryDate":viewExpiryDate, @"viewKeywords":viewKeywords};
NSArray *constraint_H2 = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|-10-[viewSelectCategory(40)]-10-[viewExpiryDate(40)]-10-[viewKeywords(100)]"] options:0 metrics:nil views:viewsDictionary2];
NSArray *constraint_V2 = [NSLayoutConstraint constraintsWithVisualFormat: [NSString stringWithFormat:@"H:|-10-[viewSelectCategory]-10-|"] options:0 metrics:nil views:viewsDictionary2];
[viewAddAd addConstraints:constraint_H2];
[viewAddAd addConstraints:constraint_V2];
NSDictionary *viewsDictionary3 = @{@"viewExpiryDate":viewExpiryDate};
NSArray *constraint_V3 = [NSLayoutConstraint constraintsWithVisualFormat: [NSString stringWithFormat:@"H:|-10-[viewExpiryDate]-10-|"] options:0 metrics:nil views:viewsDictionary3];
[viewAddAd addConstraints:constraint_V3];
NSDictionary *viewsDictionary9 = @{@"viewKeywords":viewKeywords};
NSArray *constraint_V9 = [NSLayoutConstraint constraintsWithVisualFormat: [NSString stringWithFormat:@"H:|-10-[viewKeywords]-10-|"] options:0 metrics:nil views:viewsDictionary9];
[viewAddAd addConstraints:constraint_V9];
textFieldSelectCategory = [UITextField new];
textFieldSelectCategory.backgroundColor = [UIColor colorWithRed:213.0/255 green:210.0/255 blue:210.0/255 alpha:1.0];
textFieldSelectCategory.translatesAutoresizingMaskIntoConstraints = NO;
textFieldSelectCategory.placeholder = @"Select Category";
textFieldSelectCategory.delegate = self;
[viewSelectCategory addSubview:textFieldSelectCategory];
NSDictionary *viewsDictionary6 = @{@"textFieldSelectCategory":textFieldSelectCategory};
NSArray *constraint_H6 = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|-0-[textFieldSelectCategory]-0-|"] options:0 metrics:nil views:viewsDictionary6];
NSArray *constraint_V6 = [NSLayoutConstraint constraintsWithVisualFormat: [NSString stringWithFormat:@"H:|-0-[textFieldSelectCategory]-0-|"] options:0 metrics:nil views:viewsDictionary6];
[viewSelectCategory addConstraints:constraint_H6];
[viewSelectCategory addConstraints:constraint_V6];
UIView *viewBorder3 = [UIView new];
viewBorder3.backgroundColor = [UIColor colorWithRed:201.0/255 green:198.0/255 blue:1- 98.0/255 alpha:1.0];
viewBorder3.translatesAutoresizingMaskIntoConstraints = NO;
[viewSelectCategory addSubview:viewBorder3];
NSDictionary *viewsDictionary7 = @{@"viewBorder3":viewBorder3};
NSArray *constraint_H7 = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:[viewBorder3(1)]-1-|"] options:0 metrics:nil views:viewsDictionary7];
NSArray *constraint_V7 = [NSLayoutConstraint constraintsWithVisualFormat: [NSString stringWithFormat:@"H:|-0-[viewBorder3]-0-|"] options:0 metrics:nil views:viewsDictionary7];
[viewSelectCategory addConstraints:constraint_H7];
[viewSelectCategory addConstraints:constraint_V7];
textFieldExpiryDate = [UITextField new];
textFieldExpiryDate.backgroundColor = [UIColor colorWithRed:213.0/255 green:210.0/255 blue:210.0/255 alpha:1.0];
textFieldExpiryDate.translatesAutoresizingMaskIntoConstraints = NO;
textFieldExpiryDate.placeholder = @"Enter Expiry Date";
textFieldExpiryDate.delegate = self;
[viewExpiryDate addSubview:textFieldExpiryDate];
NSDictionary *viewsDictionary4 = @{@"textFieldExpiryDate":textFieldExpiryDate};
NSArray *constraint_H4 = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|-0-[textFieldExpiryDate]-0-|"] options:0 metrics:nil views:viewsDictionary4];
NSArray *constraint_V4 = [NSLayoutConstraint constraintsWithVisualFormat: [NSString stringWithFormat:@"H:|-0-[textFieldExpiryDate]-0-|"] options:0 metrics:nil views:viewsDictionary4];
[viewExpiryDate addConstraints:constraint_H4];
[viewExpiryDate addConstraints:constraint_V4];
UIView *viewBorder2 = [UIView new];
viewBorder2.backgroundColor = [UIColor colorWithRed:201.0/255 green:198.0/255 blue:1- 98.0/255 alpha:1.0];
viewBorder2.translatesAutoresizingMaskIntoConstraints = NO;
[viewExpiryDate addSubview:viewBorder2];
NSDictionary *viewsDictionary5 = @{@"viewBorder2":viewBorder2};
NSArray *constraint_H5 = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:[viewBorder2(1)]-1-|"] options:0 metrics:nil views:viewsDictionary5];
NSArray *constraint_V5 = [NSLayoutConstraint constraintsWithVisualFormat: [NSString stringWithFormat:@"H:|-0-[viewBorder2]-0-|"] options:0 metrics:nil views:viewsDictionary5];
[viewExpiryDate addConstraints:constraint_H5];
[viewExpiryDate addConstraints:constraint_V5];
textViewKeywords = [UITextView new];
textViewKeywords.backgroundColor = [UIColor colorWithRed:213.0/255 green:210.0/255 blue:210.0/255 alpha:1.0];
textViewKeywords.translatesAutoresizingMaskIntoConstraints = NO;
//textViewKeywords.placeholder = @"Enter Expiry Date";
textViewKeywords.delegate = self;
[viewKeywords addSubview:textViewKeywords];
NSDictionary *viewsDictionary10 = @{@"textViewKeywords":textViewKeywords};
NSArray *constraint_H10 = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|-0-[textViewKeywords]-0-|"] options:0 metrics:nil views:viewsDictionary10];
NSArray *constraint_V10 = [NSLayoutConstraint constraintsWithVisualFormat: [NSString stringWithFormat:@"H:|-0-[textViewKeywords]-0-|"] options:0 metrics:nil views:viewsDictionary10];
[viewKeywords addConstraints:constraint_H10];
[viewKeywords addConstraints:constraint_V10];
UIView *viewBorder4 = [UIView new];
viewBorder4.backgroundColor = [UIColor colorWithRed:201.0/255 green:198.0/255 blue:1- 98.0/255 alpha:1.0];
viewBorder4.translatesAutoresizingMaskIntoConstraints = NO;
[viewKeywords addSubview:viewBorder4];
NSDictionary *viewsDictionary11 = @{@"viewBorder4":viewBorder4};
NSArray *constraint_H11 = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:[viewBorder4(1)]-1-|"] options:0 metrics:nil views:viewsDictionary11];
NSArray *constraint_V11 = [NSLayoutConstraint constraintsWithVisualFormat: [NSString stringWithFormat:@"H:|-0-[viewBorder4]-0-|"] options:0 metrics:nil views:viewsDictionary11];
[viewKeywords addConstraints:constraint_H11];
[viewKeywords addConstraints:constraint_V11];
//NSArray *constraint_POS_V2 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[viewAddAd]" options:0 metrics:nil views:viewsDictionary];
//NSArray *constraint_POS_H2 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[viewAddAd]" options:0 metrics:nil views:viewsDictionary];
//[viewAddAd addConstraints:constraint_POS_H2];
//[viewAddAd addConstraints:constraint_POS_V2];
//scrollAd.contentSize = viewAddAd.frame.size;
return viewAddAd;
}
答案 0 :(得分:0)
这可以解决您的问题。请尝试UIScrollView的插图,将其底部插入设置为比view.size.height更长,如下所示:
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bonds];
[scrollView setContentSize:CGSizeMake(scrollView.bonds.size.width, scrollView.bounds.size.height * 3)];
[self.view addSubView:scrollView];
答案 1 :(得分:0)
-(void)viewDidLayoutSubviews
{
//[scrollAd setContentSize:CGSizeMake(320, 500)];
UIEdgeInsets currentInsets = self.scrollAd.contentInset;
self.scrollAd.contentInset = (UIEdgeInsets){
.top = self.topLayoutGuide.length,
.bottom = currentInsets.bottom,
.left = currentInsets.left,
.right = currentInsets.right
};
}
这就是诀窍