UITableViews 水平滚动视图中的额外空白

Posted

技术标签:

【中文标题】UITableViews 水平滚动视图中的额外空白【英文标题】:Extra White Space in Horizontal Scrollview of UITableViews 【发布时间】:2016-02-29 18:45:35 【问题描述】:

这是我的代码:

- (void)viewDidLoad 
    [super viewDidLoad];

    [self.view setBackgroundColor:[self colorWithHexString:@"d944f7"]];
    self.pageControl = [[UIPageControl alloc] init];
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 0, self.view.frame.size.width - 20, self.view.frame.size.height)];
    self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;

    self.scrollView.delegate = self;
    self.scrollView.clipsToBounds = NO;
    self.scrollView.pagingEnabled = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.scrollView.contentInset = UIEdgeInsetsMake(0,0,0,0);
    [self.view addSubview:self.scrollView];


- (void)reloadData

    self.pageControl.numberOfPages = self.sectionsArray.count;
    self.pageControl.currentPage = 0;
    self.pageControl.pageIndicatorTintColor = [UIColor colorWithRed:(rand()%256)/256.0 green:(rand()%256)/256.0 blue:(rand()%256)/256.0 alpha:1];
    self.pageControl.currentPageIndicatorTintColor = [UIColor colorWithRed:(rand()%256)/256.0 green:(rand()%256)/256.0 blue:(rand()%256)/256.0 alpha:1];
    self.pageControl.center = CGPointMake(self.view.frame.size.width/2.0, self.view.frame.size.height - 20);
    self.pageControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    [self.view addSubview:self.pageControl];
    self.scrollView.contentOffset = CGPointMake(0, 0);
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width*self.pageControl.numberOfPages, self.scrollView.frame.size.height);

    NSMutableArray *array = [NSMutableArray array];
    NSMutableArray *mvcs = [NSMutableArray array];
    NSMutableArray *shadows = [NSMutableArray array];
    for (int i = 0 ; i < self.pageControl.numberOfPages; i ++) 
        UIView *shadow = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width - 20, self.scrollView.frame.size.height - 80)];
        shadow.backgroundColor = [UIColor grayColor];
        shadow.alpha = 1;
        CGPoint pageCenter = CGPointMake((i + 0.5)*self.scrollView.frame.size.width, self.scrollView.frame.size.height/2.0);
        shadow.center = pageCenter;
       // [shadows addObject:shadow];
        //[self.scrollView addSubview:shadow];

        MDIMovingView *mv = [[MDIMovingView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width - 5, self.scrollView.frame.size.height - 80)];
        mv.identity = 1;
        mv.delegate = self;
        mv.moveEnabled=NO;
        mv.backgroundColor = [self colorWithHexString:@"e6e6e6"];
        mv.center = pageCenter;
        mv.layer.cornerRadius=10;
        mv.clipsToBounds=NO;
        [array addObject:mv];
        [self.scrollView addSubview:mv];


        UIView *topHeader=[[UIView alloc] initWithFrame:CGRectMake(0, 0, mv.frame.size.width, 50)];

        [topHeader setRoundedCorners:UIRectCornerTopLeft|UIRectCornerTopRight radius:10.0];
        [topHeader setBackgroundColor:[self colorWithHexString:@"dca2e8"]];
        [mv addSubview:topHeader];

        UIView *invisibleSeperator =[[UIView alloc] initWithFrame:CGRectMake(0, 50, mv.frame.size.width, 2)];
        [invisibleSeperator setBackgroundColor:[self colorWithHexString:@"d944f7"]];
        [mv addSubview:invisibleSeperator];


        MDITableViewController *vc = [[MDITableViewController alloc] init];
        [mvcs addObject:vc];

        vc.objects = self.sortedTasksArray[i];
        vc.delegate = self;
        vc.dataSoure = self;
        vc.movingViewDelegate = self;
        [self addChildViewController:vc];
        vc.view.frame = CGRectMake(5, 60, mv.frame.size.width - 10, mv.frame.size.height - 80);
        [mv addSubview:vc.view];
    
    self.shadowViews = shadows;
    self.movingViews = array;
    self.tableVCs = mvcs;

这是它的作用——注意导航栏和移动视图之间的空间:

【问题讨论】:

您的代码有许多硬编码值。会不会是您的MDITableViewController 框架中的60?在发布之前,您应该尝试从代码中删除尽可能多的不必要的细节,以便更容易追踪问题。 发生的一些奇怪的事情是,当我将 -80 从移动视图减少到 -40 的高度时,它不仅会变长而且会向上移动,这让我觉得滚动视图中的移动视图会自动居中? 【参考方案1】:

你必须在你的视图控制器中禁用自动调整滚动视图插入

[self setAutomaticallyAdjustsScrollViewInsets:NO];

或在故事板中 取消选中该选项

【讨论】:

1.全部以编程方式完成 2. 我发布的代码中已经包含该代码。 请不要对我投反对票,因为您没有花时间阅读和/或没有好的答案。

以上是关于UITableViews 水平滚动视图中的额外空白的主要内容,如果未能解决你的问题,请参考以下文章

ScrollView 或 UICollectionView 中的 UItableViews? [关闭]

自 IOS 8 以来使用自动布局和滚动视图的额外顶部空白

UIScrollview 底部的额外空白区域

如何在滚动视图中进行多个表格视图

带有自动布局的 UIScrollView 中的 UITableViews

SwiftUI:水平滚动视图我能够上下拉图像