显示具有窗口级别的视图UIWindowLevelStatusBar重新签名当前的第一响应者

时间:2015-04-16 00:01:57

标签: ios ios7 uiview uiview-hierarchy

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

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];

1 个答案:

答案 0 :(得分: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];