【问题标题】:Getting black screen when app returning from background应用程序从后台返回时出现黑屏
【发布时间】:2017-02-24 16:52:57
【问题描述】:

我正在处理我的应用程序上的一个棘手错误。我有一个聊天视图控制器,用于处理键盘显示和隐藏。但奇怪的部分是在将应用置于后台时。

这是进入后台之前的视图控制器(点击主页按钮)

这是从后台返回时的应用

如果我使用主页按钮将应用程序置于后台,黑屏只会出现一秒钟,但如果我阻止手机然后解锁,黑屏会一直存在。

这就是我处理键盘的方式

- (void)keyboardWillShowWithRect:(CGRect)rect
{
  //We adjust inverted insets because tableview will be inverted
  CGFloat displacementDistance = rect.size.height - self.composeBarView.bounds.size.height;
  self.tableView.contentInset = UIEdgeInsetsMake(self.composeBarView.bounds.size.height, 0, displacementDistance, 0);
  [self.view setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y - displacementDistance, self.view.frame.size.width, self.view.frame.size.height)];
}

- (void)keyboardWillDismissWithRect:(CGRect)rect
{
  //We adjust inverted insets because tableview will be inverted
  CGFloat displacementDistance = rect.size.height - self.composeBarView.bounds.size.height;
  self.tableView.contentInset = UIEdgeInsetsMake(self.composeBarView.bounds.size.height, 0, 0, 0);
  [self.view setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + displacementDistance, self.view.frame.size.width, self.view.frame.size.height)];
}

用于显示消息的 tableView 被反转以便在底部显示第一条消息。这是我的做法:

- (void)setupTableView
{
  self.tableView.delegate = self;
  self.tableView.dataSource = self;
  //We adjust inverted insets because tableview will be inverted
  self.tableView.contentInset = UIEdgeInsetsMake(self.composeBarView.bounds.size.height, 0, 0, 0);
  self.tableView.rowHeight = UITableViewAutomaticDimension;
  self.tableView.estimatedRowHeight = 500.0;
  self.tableView.sectionFooterHeight = 30.0;
  self.tableView.showsVerticalScrollIndicator = NO;
  [self.tableView registerNib:[UINib nibWithNibName:@"MessagesDateHeader" bundle:nil] forHeaderFooterViewReuseIdentifier:@"MessagesDateHeader"];
  
  // Table View is inverted so we display last messages at the bottom instead of top
  self.tableView.transform = CGAffineTransformMakeRotation(-M_PI);
  UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapOnTableView)];
  tap.numberOfTapsRequired = 1;
  [self.tableView addGestureRecognizer:tap];
}

另外,我将self.definesPresentationContext = YES; 设置为viewDidLoad

谁能告诉我发生了什么?

谢谢

【问题讨论】:

  • 试试 Xcode 的视图调试功能。它应该向您展示它认为正在展示的视图及其关系。
  • 你想用self.tableView.transform = CGAffineTransformMakeRotation(-M_PI)实现什么?这会旋转视图,但不会更改消息的顺序
  • 在后台应用之前。检查您的关键字是否出现在屏幕上。对于关键字dismiss方法或者view down可以使用[self.view endEditing:YES];
  • @lufritz 不,它不会改变顺序。我所做的就是将 tableview 倒置以模仿和反转 tableview。不是问题,但我也将这种转换再次应用于我的单元格和部分标题,以避免看到内容颠倒。
  • 可能是黑屏的原因。你为什么不直接遍历数据数组呢?转换对cpu比较重,没有必要。

标签: ios objective-c iphone


【解决方案1】:

我曾经遇到过类似的问题。问题是当键盘根据其框架出现/消失时调整 tableView 的大小。 我所做的是在 tableView 的底部和 superView 之间设置一个约束,然后根据键盘的出现/消失来更改它的常量。

除了依赖keyboardWillShowWithRect:keyboardWillDismissWithRect:,你可以使用listen to UIKeyboardWillChangeFrameNotification 并像下面这样处理它

- (void) keyboardWillChangeFrame:(NSNotification *)notification {
    NSDictionary *userInfo = [notification userInfo];
    CGRect keyboardFrameEnd = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    keyboardFrameEnd = [self.view convertRect:keyboardFrameEnd toView:nil];

    [UIView animateWithDuration:[userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]
                          delay:0.f
                        options:[userInfo[UIKeyboardAnimationCurveUserInfoKey] intValue] << 16
                     animations:^{
        self.bottomConstraint.constant = CGRectGetHeight(self.view.frame) - keyboardFrameEnd.origin.y;
        [self.view layoutIfNeeded];
                   } completion:nil];
}

[self.view layoutIfNeeded] 用于让视图显示整个动画

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-13
    • 2022-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多