1

我在尝试在应用程序中显示应用内横幅(状态栏上方)时遇到问题。问题是,当我在文本字段中输入文本时,如果出现横幅,它会移除键盘,然后一旦横幅消失(它在计时器上),键盘就会再次出现。有没有办法在状态栏上方有一个横幅视图,同时如果键盘是第一响应者,键盘不会消失。

InAppNotificationView* _sharedPushView = nil;
NSArray              * nibArr          = [[NSBundle mainBundle] loadNibNamed: @"InAppNotificationView" owner: self options: nil];
for (id currentObject in nibArr)
{
    if ([currentObject isKindOfClass: [InAppNotificationView class]])
    {
        _sharedPushView = (InAppNotificationView*) currentObject;
        break;
    }
}
_sharedPushView.delegate = self;

[self.displayedPushViews addObject: _sharedPushView];
                   _topView.window.windowLevel = UIWindowLevelStatusBar;
                   [UIView animateWithDuration: 0.25
                                    animations: ^
                                    {
                                        CGPoint centerPoint  = _sharedPushView.center;
                                        centerPoint.y       += _sharedPushView.frame.size.height;
                                        _sharedPushView.center      = centerPoint;
                                    }
                                    completion: nil];

                   [self.closeTimer invalidate];
                   self.closeTimer = nil;
                   self.closeTimer = [NSTimer scheduledTimerWithTimeInterval: 3.0f
                                                                      target: self
                                                                    selector: @selector(close)
                                                                    userInfo: nil
                                                                     repeats: NO];
4

1 回答 1

1

我找到了解决方案。创建一个自定义 UIWindow 并将 UIView 作为子视图添加到它。这为我解决了问题。另一件事是您必须覆盖“hitTest:withEvent”方法,因为默认情况下所有触摸都将转到窗口。因此,对于自定义窗口不应该处理的点击,它必须进一步委派它。

我创建了一个自定义 UIWindow 类:

@interface InAppNotificationWindow : UIWindow
@property (assign, nonatomic) CGFloat notificationHeight;
@end
@implementation InAppNotificationWindow

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
      if (point.y > 0 && point.y < self.notificationHeight)
      {
          return [super hitTest: point withEvent: event];
      }
     return nil;
}
@end

在我的控制器文件中

- (InAppNotificationWindow* ) notificationWindow
{
     if (_notificationWindow == nil)
     {
            _notificationWindow = [[InAppNotificationWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
            _notificationWindow.backgroundColor        = [UIColor clearColor];
           _notificationWindow.userInteractionEnabled = YES;
           _notificationWindow.hidden                 = NO;
          _notificationWindow.autoresizingMask       = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
            _notificationWindow.windowLevel            = UIWindowLevelStatusBar;
     }

return _notificationWindow;

}

然后将自定义视图作为子视图添加到自定义窗口。

 [self.notificationWindow addSubview: _topView];
于 2015-04-17T22:14:14.540 回答