Ios 在导航栏中添加自定义视图,如whatsapp

Ios 在导航栏中添加自定义视图,如whatsapp,ios,uinavigationbar,custom-view,uistatusbar,Ios,Uinavigationbar,Custom View,Uistatusbar,我想创建自定义导航栏,如WhatsApp用于在应用程序中显示呼叫指示器,如下所示 @property (nonatomic) UIView *navigationBarTopView; UIWindow *window = [UIApplication sharedApplication].keyWindow; if (_navigationBarTopView == nil) { _navigationBarTopView = [[UIView alloc] initWithFram

我想创建自定义导航栏,如WhatsApp用于在应用程序中显示呼叫指示器,如下所示

@property (nonatomic) UIView *navigationBarTopView;

UIWindow *window = [UIApplication sharedApplication].keyWindow;
if (_navigationBarTopView == nil) {
    _navigationBarTopView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, window.frame.size.width, 60.0)];
        [_navigationBarTopView setBackgroundColor:[UIColor colorWithRed:76.0/255.0 green:217.0/255.0 blue:100.0/255.0 alpha:1]];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, _navigationBarTopView.frame.size.height - 15, window.frame.size.width, 10)];
    [label setText:@"Touch to return to call"];
    [label setTextColor:[UIColor whiteColor]];
    [label setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleFootnote]];
    [label setTextAlignment:NSTextAlignmentCenter];
    [_navigationBarTopView addSubview:label];

    //The setup code (in viewDidLoad in your view controller)
    UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    [_navigationBarTopView addGestureRecognizer:singleFingerTap];

    UITapGestureRecognizer *singleFingerTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    [label addGestureRecognizer:singleFingerTap1];
}

[window addSubview:_navigationBarTopView];

我已经成功地添加了如上所述的视图,但它没有响应,因为我无法检测到状态栏上的触摸。我只能触摸“触摸返回呼叫”下面的部分

代码如下所示

@property (nonatomic) UIView *navigationBarTopView;

UIWindow *window = [UIApplication sharedApplication].keyWindow;
if (_navigationBarTopView == nil) {
    _navigationBarTopView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, window.frame.size.width, 60.0)];
        [_navigationBarTopView setBackgroundColor:[UIColor colorWithRed:76.0/255.0 green:217.0/255.0 blue:100.0/255.0 alpha:1]];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, _navigationBarTopView.frame.size.height - 15, window.frame.size.width, 10)];
    [label setText:@"Touch to return to call"];
    [label setTextColor:[UIColor whiteColor]];
    [label setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleFootnote]];
    [label setTextAlignment:NSTextAlignmentCenter];
    [_navigationBarTopView addSubview:label];

    //The setup code (in viewDidLoad in your view controller)
    UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    [_navigationBarTopView addGestureRecognizer:singleFingerTap];

    UITapGestureRecognizer *singleFingerTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    [label addGestureRecognizer:singleFingerTap1];
}

[window addSubview:_navigationBarTopView];
我还尝试在状态栏上添加触摸,如下所示,但它不起作用


此外,导航栏也不会出现问题。我已尝试设置
keyWindow
frame。但这也不起作用。

我也尝试了singleViewcontroller和UITabbar Controller,我附加的示例项目,用于我遵循的状态栏点击操作


UIStatusBar的优先级高于应用程序的窗口,因此您不会通过状态栏获得任何触摸事件

要通过状态栏获取触摸事件,您需要一个窗口级别高于UIStatusBar的窗口

请尝试以下代码:

#import "AppDelegate.h"

@interface AppDelegate ()

@property (nonatomic) UIWindow *buttonWindow;
@property (nonatomic) UIWindow *textWindow;
@property (nonatomic) CGFloat callViewHeight;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
        if (@available(iOS 11.0, *)) {
        if (([[[UIApplication sharedApplication] delegate] window].safeAreaInsets.top > 20.0)) {
            self.callViewHeight = 55.0f;
        } else {
            self.callViewHeight = 35.0f;
        }
    } else {
        self.callViewHeight = 35.0f;
    }
    return YES;
}

-(void)showVideoCallButton{
    self.textWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.callViewHeight)];
    self.textWindow.windowLevel = self.window.windowLevel + 1;
    self.textWindow.hidden = FALSE;

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.callViewHeight)];
    view.backgroundColor = [UIColor colorWithRed:76.0/255.0 green:217.0/255.0 blue:100.0/255.0 alpha:1];
    view.clipsToBounds = TRUE;
    UILabel *lblName = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.callViewHeight)];
    lblName.textAlignment = NSTextAlignmentCenter;
    [lblName setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleFootnote]];
    lblName.text = @"";

    UILabel *lblTouch = [[UILabel alloc] initWithFrame:CGRectMake(0, self.callViewHeight - 20.0f, [UIScreen mainScreen].bounds.size.width, 20.0f)];
    lblTouch.textAlignment = NSTextAlignmentCenter;
    [lblTouch setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleFootnote]];
    lblTouch.text = @"Touch to return to call";
    lblTouch.textColor = UIColor.whiteColor;

    [view addSubview:lblTouch];
    [view addSubview:lblName];

    [self.textWindow addSubview:view];

    self.buttonWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.callViewHeight)];
    self.buttonWindow.windowLevel = UIWindowLevelStatusBar + 1;
    self.buttonWindow.hidden = FALSE;

    UIButton *button = [[UIButton alloc] initWithFrame:lblName.bounds];
    [button setTitle:@"" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonTouched) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonWindow addSubview:button];

    self.textWindow.transform = CGAffineTransformMakeTranslation(0, -self.callViewHeight);
    self.buttonWindow.transform = CGAffineTransformMakeTranslation(0, -self.callViewHeight);

    [UIView animateWithDuration:0.25
                     animations:^{
                         self.textWindow.transform = CGAffineTransformIdentity;
                         self.buttonWindow.transform = CGAffineTransformIdentity;
                         if (@available(iOS 11.0, *)) {
                             if (([[[UIApplication sharedApplication] delegate] window].safeAreaInsets.top > 20.0)) {
                                 self.window.frame = CGRectMake(0, self.callViewHeight - 40.0f, self.window.bounds.size.width, self.window.bounds.size.height - self.callViewHeight + 40.0f);
                             } else {
                                 self.window.frame = CGRectMake(0, self.callViewHeight - 20.0f, self.window.bounds.size.width, self.window.bounds.size.height - self.callViewHeight + 20.0f);
                             }
                         } else {
                             self.window.frame = CGRectMake(0, self.callViewHeight - 20.0f, self.window.bounds.size.width, self.window.bounds.size.height - self.callViewHeight + 20.0f);
                         }
                     }];
}

-(void)hideVideoCallButton{

    [UIView animateWithDuration:0.25
                     animations:^{
                         self.textWindow.transform = CGAffineTransformMakeTranslation(0, -self.callViewHeight);
                         self.buttonWindow.transform = CGAffineTransformMakeTranslation(0, -self.callViewHeight);
                         self.window.frame = [UIScreen mainScreen].bounds;
                     } completion:^(BOOL finished) {

                         dispatch_async(dispatch_get_main_queue(), ^{
                             self.buttonWindow.hidden = TRUE;
                             self.buttonWindow = nil;

                             self.textWindow.hidden = TRUE;
                             self.textWindow = nil;
                         });

                     }];
}

-(void)buttonTouched{
    NSLog(@"Button Touched");
}

@end

您确定这不是系统
调用栏
?请尝试此
[self.navigationController.view addSubview:\u navigationBarTopView]它是系统生成的导航栏。它在与任何人通话时自动显示。呼叫可以是普通或VoIP(由SDK管理)。如果需要,您可以将自定义视图添加到导航栏。如果您想创建相同的视图,这是apple提供的默认行为,如将子视图添加到顶部的navigationController.viewviewcontroller@iMHiteshSurani我不会把它加入应用程序。我已经实现了同样的Twilio语音SDK。因此,我创建了自定义视图,并将其添加为屏幕顶部的UIWindow。仅供参考:具有可用安全区域呼叫功能的设备与非安全区域设备不同。它将在状态栏中显示一个小的呼叫图标bar@Anbu.Karthik我已经实现了这段代码,但使用这段代码我无法接触到状态栏。此外,我有一个复杂的结构,其中包括一个选项卡栏和4个导航栏控制器。所以很难在应用程序中进行管理。要在状态栏上显示触摸事件,我正在使用此解决方案。但它在选项卡栏上不起作用。它与单视图控制器配合良好。请使用此库进行同样的操作。我知道,可能需要一些定制,但它将易于使用和评级是好的@JayeshSojitra-我更新了答案,它也适用于单视图控制器和UITabbar控制器,请检查谢谢@iDhaval此解决方案有效。我已经做了一些更改,使其与iPhone X+设备兼容。您好,有没有针对swift的解决方案?@iDhaval,我尝试过,但在xcode 11中不起作用。6@swift2geek你面临什么问题?