iOS UITableViewCell 内容在第一次滚动时移动

标签 ios uitableview autolayout

您可以在下面的 gif 中看到,在第一次滚动 UITableView 时,单元格的内容移动了一点点。你几乎看不到它,边距变宽了 1 个像素。我以前从未遇到过这种情况。似乎在第一次滚动之前存在一些布局问题,并在事后自行解决。 XCode 中没有警告,这些自定义单元格非常简单,没有布局代码覆盖。

我不知道从哪里开始,我该如何捕捉这个故障?

更新。我现在已经实现了一个明显的解决方法:

- (void)scrollTableToFixGlitch {

   [self.tableView setContentOffset:CGPointMake(0, 1)];
   [self.tableView setContentOffset:CGPointMake(0, -1)];

}
- (void)viewDidLoad {

   [super viewDidLoad];

   [self scrollTableToFixGlitch];
}

仍在调查问题。我试过通用的 UITableViewCells,没有任何改变。似乎是 View Controller 的 Root View 或 tableview 布局的问题,而不是表格单元格的问题。

更新 2.

我排除了所有动画的问题,问题出在不同区域的某个地方。在大大简化的项目中很容易重现故障。我的标签栏 Controller 基于 MHCustomTabBarController带有自定义转场和其他一些附加功能。这是重现故障的方法。设置一个项目,将您的初始 VC 嵌入到导航 Controller 中。下一个 Controller MHCustomTabBarController 或子类被推送到导航堆栈。选项卡栏中第一个可见的 VC 是通用 TableView Controller 。而已。仅当标签栏 Controller 被插入导航堆栈时才会出现故障。

下面是一些我认为对标签栏 Controller 很重要的代码:

-(void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

   if (self.childViewControllers.count < 1) {
    [self performSegueWithIdentifier:@"viewController1" sender:[self.buttons objectAtIndex:0]];
  }
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if (![segue isKindOfClass:[MHTabBarSegue class]]) {
        [super prepareForSegue:segue sender:sender];
        return;
    }

    self.oldViewController = self.destinationViewController;

    //if view controller isn't already contained in the viewControllers-Dictionary
    if (![self.viewControllersByIdentifier objectForKey:segue.identifier]) {
        [self.viewControllersByIdentifier setObject:segue.destinationViewController forKey:segue.identifier];
    }

    [self.buttons setValue:@NO forKeyPath:@"selected"];
    [sender setSelected:YES];
    self.selectedIndex = [self.buttons indexOfObject:sender];

    self.destinationIdentifier = segue.identifier;
    self.destinationViewController = [self.viewControllersByIdentifier objectForKey:self.destinationIdentifier];

    [[NSNotificationCenter defaultCenter] postNotificationName:MHCustomTabBarControllerViewControllerChangedNotification object:nil]; 


}

和自定义转场代码:

@implementation MHTabBarSegue
- (void) perform {


    MHCustomTabBarController *tabBarViewController = (MHCustomTabBarController *)self.sourceViewController;
    UIViewController *destinationViewController = (UIViewController *) tabBarViewController.destinationViewController;

    //remove old viewController
    if (tabBarViewController.oldViewController) {
        [tabBarViewController.oldViewController willMoveToParentViewController:nil];
        [tabBarViewController.oldViewController.view removeFromSuperview];
        [tabBarViewController.oldViewController removeFromParentViewController];
    }

    destinationViewController.view.frame = tabBarViewController.container.bounds;
    [tabBarViewController addChildViewController:destinationViewController];
    [tabBarViewController.container addSubview:destinationViewController.view];
    [destinationViewController didMoveToParentViewController:tabBarViewController];
}

@end

更新 3

在我的研究过程中,我发现 - viewWillAppear 不会在子 Controller 出现时第一次被调用。但它在所有后续时间都被调用。

enter image description here

最佳答案

也许 scrollviews contentSize 比您的 scrollView 的框架宽(在这种情况下具体为宽度)导致双向滚动。 您可以尝试将 contentSize 宽度减小到 scrollView 的宽度和

self.scrollView.alwaysBounceHorizontal = NO;

如果这不起作用,解决方案是在 UIScrollView 委托(delegate)的帮助下以编程方式禁用水平滚动

self.scrollView.delegate = self;
[self.scrollView setShowsHorizontalScrollIndicator:NO];
//for the below UIScrollView delegate function to work do the necessary step in the bottom of my answer.
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x > 0)
    scrollView.contentOffset = CGPointMake(0, scrollView.contentOffset.y);
}

并且在您的 .h 文件中,您应该通过添加 UIScrollViewDelegate 将界面行更改为以下内容

@interface ViewController : UIViewController <UIScrollViewDelegate>

您很可能非常了解委托(delegate)部分,但对于其他人可能需要:D

关于iOS UITableViewCell 内容在第一次滚动时移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29922909/

相关文章:

ios - 点击时更改 UITableViewCell 的文本

ios - Swift Connect UITableView 用于多个子类别

ios - 从列到行的自动布局约束

ios - 使用自动布局处理 iPhone X 缺口?

iPhone 网络摄像头馈送

uitableview - Storyboard Xcode 中的 UISearchBar

objective-c - iOS:如何使用 MPMoviePlayerController

ios - 如何使用自动布局对 subview 进行编程以占用其父 View 宽度的 50% 但保持恒定高度?

iphone - Plist 文件转换 xml 文件在 iphone 中?

ios - UIImageView 内容模式不起作用