iOS 如何自动布局 3 x 屏幕宽度的视图?

iOS How to auto layout a view 3 x screen width?

我试图在 UIScrollView 中放置一个 UIView,它是屏幕宽度的 3 倍(或 UIScrollView 的宽度的 3 倍)。

UIScrollView的宽度等于屏幕宽度。

如何在自动版式中执行此操作?

为视图的宽度约束分配一个值,并在头文件中为此创建一个 IBOutlet。

在您的 m 文件的 viewDidLoad 方法中获取当前设备屏幕的宽度,乘以 3 并分配给您的约束常量。最后调用视图的 layoutIfNeeded 方法。

这是否足够清楚,还是你想让我说得更清楚?

如果您想通过纯 AutoLayout 在代码中执行此操作,请按以下步骤操作:

- (void)layoutUserInterface {
    // Placing the scrollView using AutoLayout
    [self.view addSubview:self.scrollView];
    // Note: The "H:" is optional, but I like to be clear
    [self.view addConstraints:
     [NSLayoutConstraint
      constraintsWithVisualFormat:@"H:|[scrollview]|"
      options:0
      metrics:nil
      views:@{@"scrollview": self.scrollView}]];
    [self.view  addConstraints:
     [NSLayoutConstraint
      constraintsWithVisualFormat:@"V:|[scrollview]|"
      options:0
      metrics:nil
      views:@{@"scrollview": self.scrollView}]];

    // Placing the "wideView" using AutoLayout
    [self.scrollView addConstraints:
     [NSLayoutConstraint
      constraintsWithVisualFormat:@"H:|[wideView]"
      options:0
      metrics:nil
      views:@{@"wideView": self.wideView}]];
    [self.scrollView addConstraints:
     [NSLayoutConstraint
      constraintsWithVisualFormat:@"V:|[wideView]"
      options:0
      metrics:nil
      views:@{@"wideView": self.wideView}]];
    // Setting up the 3x width constraint
    [self.scrollView addConstraint:
     [NSLayoutConstraint
      constraintWithItem:self.wideView
      attribute:NSLayoutAttributeWidth
      relatedBy:NSLayoutRelationEqual
      toItem:self.view
      attribute:NSLayoutAttributeWidth
      multiplier:3.0f
      constant:0.0f]];
    // Figure out your "wideView's" height requirements

}