2015-04-16 74 views
1

我遇到了一個問題,我試圖在應用程序內顯示應用程序內橫幅(狀態欄上方)。問題是,當我在文本框中鍵入文本時,如果橫幅出現,它將移除鍵盤,然後一旦橫幅消失(它在計時器上),鍵盤再次顯示。有沒有辦法在狀態欄上方顯示橫幅視圖,同時如果鍵盤是第一響應者,則不會使鍵盤消失。顯示窗口級別的視圖UIWindowLevelStatusBar退出當前的第一響應者

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

我找到了解決方案。創建一個自定義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]; 
相關問題