【问题标题】:UIPageControl doesn't obey it's constraints inside UIScrollViewUIPageControl 不遵守它在 UIScrollView 中的约束
【发布时间】:2014-02-11 02:45:10
【问题描述】:

我有一个 UIScrollView,我的滚动视图中有很多视图。我正在使用自动布局,我所有的视图都以相同的方式布局:左侧和顶部间距到超级视图、宽度和高度设置。一切都滚动得很好,但是我的页面控件保持在任何位置。它不会与滚动视图中的其他元素一起滚动。是的,我确实检查了页面控件 是否像其他元素一样在滚动视图内,是的,我已经四重检查了页面控件的约束。它只是不会滚动。可能是什么问题呢?有标签,另一个滚动视图,文本视图,图像视图,它们都可以完美滚动,只是页面视图有问题。 Xcode/iOS SDK 是否存在错误,还是我遗漏了什么?

更新:我的滚动视图中的所有视图都在容器视图中。滚动视图和容器视图的translatesAutoresizingMaskIntoConstraints 属性都设置为NO。只有页面控件不遵守它的约束。这是 Interface Builder 的屏幕截图:

【问题讨论】:

    标签: ios ios7 uiscrollview interface-builder xcode5


    【解决方案1】:

    我让它工作了:

    1) 将所有视图/控件放入 containerView 中并将其声明为属性。

    2) 添加此代码:

    -(void)viewDidLayoutSubviews{
        [super viewDidLayoutSubviews];
        self.scrollView.contentSize = self.containerView.bounds.size;
    }
    
    - (void)viewDidLoad{
        [super viewDidLoad];
    
        //added with containerview from g8productions
        self.containerView =  [[UIView alloc] initWithFrame:CGRectMake(36, 59, 900, 1200)];
        self.scrollView.translatesAutoresizingMaskIntoConstraints = NO;
        [self.scrollView addSubview:self.containerView];
        [self.scrollView setContentSize:self.containerView.bounds.size];
    }
    

    希望这些解决方案中的任何一个都适合您!

    【讨论】:

    • 我已经在滚动视图中的容器视图中获得了所有内容,并且滚动视图和容器视图的translatesAutoresizingMaskIntoConstraints 属性都设置为NO。只有页面控件行为不端。
    • 您确定它在正确的视图内还是错误地不在另一个视图内?您是否在使用 Scene Inspector 来查看谁在谁里面?
    • 是的,我已经四次检查了一切是否应该如此。我添加了一个屏幕截图。
    【解决方案2】:

    要使用 Auto Layout 调整滚动视图的框架大小,必须明确限制滚动视图的宽度和高度,或者滚动视图的边缘必须绑定到其子树之外的视图。

    https://developer.apple.com/library/ios/releasenotes/General/RN-iOSSDK-6_0/index.html

    如果你能转储 Autolayout 那就最好了。

    查看该链接,他们在底部有一个 Pure AutoLayout 示例。

    基本使用这个代码模式:

    @interface ViewController () {
        UIScrollView *scrollView;
        UIImageView *imageView;
    }
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad{
        [super viewDidLoad];
    
        scrollView.translatesAutoresizingMaskIntoConstraints = NO;
        imageView.translatesAutoresizingMaskIntoConstraints = NO;
    
        [scrollView addSubview:imageView];
        [self.view addSubview:scrollView];
    
    
        NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(scrollView,imageView);
    
    
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|" options:0 metrics: 0 views:viewsDictionary]];
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]|" options:0 metrics: 0 views:viewsDictionary]];
    
        [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[imageView]|" options:0 metrics: 0 views:viewsDictionary]];
        [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[imageView]|" options:0 metrics: 0 views:viewsDictionary]];
    }
    

    【讨论】:

      【解决方案3】:

      我做了类似的事情,并在滚动视图委托方法中更新了页面控件:

      -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
      {
          // If tag == 0 means that is the scrollview for gallery
          if (scrollView.tag == 0)
          {
              self.pageControllGallery.currentPage = self.sv1ScrollViewGallery.contentOffset.x/320;
          }
          else
          {
              // Change the bottom label text
              int page = (scrollView.contentOffset.x / 320);
      
              [UIView animateWithDuration:0.3 animations:^{
                  self.labelBottom.alpha = 0.0f;
                  self.labelBottom.text = [self.arrayOfScrollviews objectAtIndex:page];
                  self.labelBottom.alpha = 1.0f;
              }];
          }
      }
      

      我计算页面并在 PageControll 中设置该页面。

      不要忘记在 .h 和元素中设置 Scrollview 委托。

      【讨论】:

      • 我尝试以类似的方式更新页面控件的约束,并意识到它们在运行时变为 nil。页面控件删除自己的约束。
      • 你声明你的 PageControl 强大了吗?
      • 它很弱,我已将其更改为强,但没有任何改变。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-21
      • 2015-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多