ScrollViwe 使用autolayout

  • Pure Auto Layout approach:

    1. Set translatesAutoresizingMaskIntoConstraints to NO on all views involved.

    2. Position and size your scroll view with constraints external to the scroll view.

    3. Use constraints to lay out the subviews within the scroll view, being sure that the constraints tie to all four edges of the scroll view and do not rely on the scroll view to get their size.

      A simple example would be a large image view, which has an intrinsic content size derived from the size of the image. In the viewDidLoad method of your view controller, you would include code like the following:

      UIScrollView *scrollView = [[UIScrollView alloc] init];
      UIImageView *imageView = [[UIImageView alloc] init];
      [imageView setImage:[UIImage imageNamed:"MyReallyBigImage"]];
      [self.view addSubview:scrollView];
      [scrollView addSubview:imageView];
       
      scrollView.translatesAutoresizingMaskIntoConstraints = NO;
      imageView.translatesAutoresizingMaskIntoConstraints = NO;
       
      NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(scrollView,imageView);
      [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|" options:0 metrics: 0 viewsDictionary:viewsDictionary]];
      [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]|" options:0 metrics: 0 viewsDictionary:viewsDictionary]];
      [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[imageView]|" options:0 metrics: 0 viewsDictionary:viewsDictionary]];
      [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[imageView]|" options:0 metrics: 0 viewsDictionary:viewsDictionary]];

      This would give you a scroll view that resized as the view controller’s view resized (such as on device rotation), and the image view would be a scrolling subview. You don’t have to set the content size of the scroll view.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值