Ios UIScrollView的所有图像都放在开头

Ios UIScrollView的所有图像都放在开头,ios,objective-c,uiscrollview,Ios,Objective C,Uiscrollview,我不熟悉iOS编程。我希望使用编码生成所有控件,然后应用约束以实现自动调整大小功能。我几乎达到了我的要求,除了一个问题,那就是我的UIScrollView的所有图像都被放置在最开始的位置,而UIScrollView的其余部分保持为空。我想我的约束条件有些问题,目前我无法解决。 这是我的密码 self.bgView.image = [UIImage imageNamed:@"bg.png"]; NSDictionary *viewDictionary = @{@"bgImage":self.bg

我不熟悉iOS编程。我希望使用编码生成所有控件,然后应用约束以实现自动调整大小功能。我几乎达到了我的要求,除了一个问题,那就是我的UIScrollView的所有图像都被放置在最开始的位置,而UIScrollView的其余部分保持为空。我想我的约束条件有些问题,目前我无法解决。 这是我的密码

self.bgView.image = [UIImage imageNamed:@"bg.png"];

NSDictionary *viewDictionary = @{@"bgImage":self.bgView,@"scrollView":self.scrollView};

NSDictionary *position = @{@"vSpacing":@0,@"hSpacing":@0};

//here I had specified the size of the background image corresponding to the view

NSArray *constraint_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-hSpacing-[bgImage]-hSpacing-|" options:0 metrics:position views:viewDictionary];

NSArray *constraint_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-vSpacing-[bgImage]-vSpacing-|" options:0 metrics:position views:viewDictionary];

[self.view addConstraints:constraint_POS_H];

[self.view addConstraints:constraint_POS_V];



    //here I am specifying the size of scroll view
    [self.view addConstraint:[NSLayoutConstraint
                              constraintWithItem:self.scrollView
                              attribute:NSLayoutAttributeWidth
                              relatedBy:NSLayoutRelationGreaterThanOrEqual
                              toItem:self.bgView
                              attribute:NSLayoutAttributeWidth
                              multiplier:1.0
                              constant:0.0]];

    [self.view addConstraint:[NSLayoutConstraint
                              constraintWithItem:self.scrollView
                              attribute:NSLayoutAttributeHeight
                              relatedBy:NSLayoutRelationEqual
                              toItem:self.bgView
                              attribute:NSLayoutAttributeHeight
                              multiplier:0.5
                              constant:0.0]];


    [self.view addConstraint:[NSLayoutConstraint
                              constraintWithItem:self.scrollView
                              attribute:NSLayoutAttributeCenterX
                              relatedBy:NSLayoutRelationEqual
                              toItem:self.bgView
                              attribute:NSLayoutAttributeCenterX
                              multiplier:1.0
                              constant:0.0]];

    [self.view addConstraint:[NSLayoutConstraint
                              constraintWithItem:self.scrollView
                              attribute:NSLayoutAttributeCenterY
                              relatedBy:NSLayoutRelationEqual
                              toItem:self.bgView
                              attribute:NSLayoutAttributeCenterY
                              multiplier:1.0
                              constant:0.0]];





    //self.view.autoresizesSubviews = YES;
    self.scrollView.pagingEnabled = YES;
    NSInteger numberOfViews = photoArray.count;
    for (int i=0; i < numberOfViews; i++) {
        CGFloat myOrigin = i * self.view.frame.size.width;
        NSLog(@"self.view.frame.size.width : %f",self.view.frame.size.width);
        UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(myOrigin, 0, self.view.frame.size.width, self.scrollView.frame.size.height)];

        [myView setTranslatesAutoresizingMaskIntoConstraints:NO];
        [self.view addSubview:myView];
        //here I am specifying the size of uiview
      [self.view addConstraint:[NSLayoutConstraint
                                  constraintWithItem:myView
                                  attribute:NSLayoutAttributeWidth
                                  relatedBy:NSLayoutRelationGreaterThanOrEqual
                                  toItem:self.scrollView
                                  attribute:NSLayoutAttributeWidth
                                  multiplier:1.0
                                  constant:0.0]];
        [self.view addConstraint:[NSLayoutConstraint
                                  constraintWithItem:myView
                                  attribute:NSLayoutAttributeHeight
                                  relatedBy:NSLayoutRelationEqual
                                  toItem:self.scrollView
                                  attribute:NSLayoutAttributeHeight
                                  multiplier:1.0
                                  constant:0.0]];

        //here I am specifying the position of uiview
         [self.view addConstraint:[NSLayoutConstraint
                                  constraintWithItem:myView
                                  attribute:NSLayoutAttributeCenterX
                                  relatedBy:NSLayoutRelationEqual
                                  toItem:self.scrollView
                                  attribute:NSLayoutAttributeCenterX
                                  multiplier:1.0
                                  constant:0.0]];
        [self.view addConstraint:[NSLayoutConstraint
                                  constraintWithItem:myView
                                  attribute:NSLayoutAttributeCenterY
                                  relatedBy:NSLayoutRelationEqual
                                  toItem:self.scrollView
                                  attribute:NSLayoutAttributeCenterY
                                  multiplier:1.0
                                  constant:0.0]];

        UIImageView *photos = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, myView.frame.size.width, self.scrollView.frame.size.height)];
        //self.photos = [UIImageView new];
        [photos setTranslatesAutoresizingMaskIntoConstraints:NO];

        photos.image = [photoArray objectAtIndex:i];
        [myView addSubview:photos];
        //here I am specifying the size of image view within scroll view
           [self.view addConstraint:[NSLayoutConstraint
                                  constraintWithItem:photos
                                  attribute:NSLayoutAttributeWidth
                                  relatedBy:NSLayoutRelationLessThanOrEqual
                                  toItem:myView
                                  attribute:NSLayoutAttributeWidth
                                  multiplier:1.0
                                  constant:0.0]];
        [self.view addConstraint:[NSLayoutConstraint
                                  constraintWithItem:photos
                                  attribute:NSLayoutAttributeHeight
                                  relatedBy:NSLayoutRelationEqual
                                  toItem:self.scrollView
                                  attribute:NSLayoutAttributeHeight
                                  multiplier:1.0
                                  constant:0.0]];

        //here I am specifying the position of the image view
        [self.view addConstraint:[NSLayoutConstraint
                                  constraintWithItem:photos
                                  attribute:NSLayoutAttributeCenterX
                                  relatedBy:NSLayoutRelationEqual
                                  toItem:self.scrollView
                                  attribute:NSLayoutAttributeCenterX
                                  multiplier:1.0
                                  constant:0.0]];
        [self.view addConstraint:[NSLayoutConstraint
                                  constraintWithItem:photos
                                  attribute:NSLayoutAttributeCenterY
                                  relatedBy:NSLayoutRelationEqual
                                  toItem:myView
                                  attribute:NSLayoutAttributeCenterY
                                  multiplier:1.0
                                  constant:0.0]];






        self.scrollView.delegate = self;
        [self.scrollView addSubview:myView];
        NSLog(@"self.myView.frame.size.width : %f",myView.frame.size.width);

    }

    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews,
                                             self.scrollView.frame.size.height);
    CGPoint scrollPoint = CGPointMake(0, 0);
   [self.scrollView setContentOffset:scrollPoint animated:YES];

    [self.view addSubview:self.scrollView];
self.bgView.image=[UIImage ImageName:@“bg.png”];
NSDictionary*viewDictionary=@{@“bgImage”:self.bgView,@“scrollView”:self.scrollView};
NSDictionary*position=@{@“vSpacing”:@0,@“hSpacing”:@0};
//在这里,我指定了与视图对应的背景图像的大小
NSArray*constraint_POS_H=[NSLayoutConstraint Constraints With VisualFormat:@“H:|-hSpacing-[bgImage]-hSpacing-|”选项:0度量:位置视图:viewDictionary];
NSArray*constraint_POS_V=[NSLayoutConstraint Constraints With VisualFormat:@“V:|-VSPACGE-[bgImage]-VSPACGE-|”选项:0度量:位置视图:viewDictionary];
[self.view addConstraints:constraint_POS_H];
[self.view addConstraints:constraint_POS_V];
//这里我指定了滚动视图的大小
[self.view addConstraint:[NSLayoutConstraint]
constraintWithItem:self.scrollView
属性:NSLAYUTATTRIBUTEWIDTH
相关方:NSLAYOUT相关性大于或等于
toItem:self.bgView
属性:NSLAYUTATTRIBUTEWIDTH
乘数:1.0
常数:0.0]];
[self.view addConstraint:[NSLayoutConstraint]
constraintWithItem:self.scrollView
属性:NSLayoutAttributeHeight
关系人:NSLayoutRelationEqual
toItem:self.bgView
属性:NSLayoutAttributeHeight
乘数:0.5
常数:0.0]];
[self.view addConstraint:[NSLayoutConstraint]
constraintWithItem:self.scrollView
属性:nsLayoutAttributeCenter
关系人:NSLayoutRelationEqual
toItem:self.bgView
属性:nsLayoutAttributeCenter
乘数:1.0
常数:0.0]];
[self.view addConstraint:[NSLayoutConstraint]
constraintWithItem:self.scrollView
属性:nsLayoutAttributeCenter
关系人:NSLayoutRelationEqual
toItem:self.bgView
属性:nsLayoutAttributeCenter
乘数:1.0
常数:0.0]];
//self.view.autoresizesSubviews=是;
self.scrollView.paginEnabled=是;
NSInteger numberOfViews=photoArray.count;
对于(int i=0;i