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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在导航栏中添加自定义视图,如whatsapp相关的知识,希望对你有一定的参考价值。

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

enter image description here

我已经成功添加了上面的视图,但它没有响应,因为我无法检测状态栏上的触摸。我只能触摸“Touch to return to call”下面的部分。

代码如下。

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

我还尝试在状态栏上添加触摸,如下所示,但它不起作用。

How do I detect touches on UIStatusBar/iPhone

导航栏也没有下降。我试图设置keyWindow框架。但这也行不通。

答案

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
另一答案

我尝试在singleViewcontroller和UITabbar Controller中,我附加here的示例项目,状态栏点击动作我跟着Flar answer

 - (void)viewDidLoad 
[super viewDidLoad];

dispatch_async(dispatch_get_main_queue(), ^(void)
    //Run UI Updates
     [self createDummyView];
);




 -(void)createDummyView

if (_navigationBarTopView == nil) 
     float statusBarHeight = [self statusBarHeight];
    CGFloat width = [UIScreen mainScreen].bounds.size.width;

    _navigationBarTopView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, self.navigationController.navigationBar.frame.size.height +  statusBarHeight)];
    [_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,width, 10)];
    [label setText:@"Touch to return to call"];
    [label setTextColor:[UIColor whiteColor]];
    [label setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleFootnote]];
    [label setTextAlignment:NSTextAlignmentCenter];
    [label setUserInteractionEnabled:YES];
    [_navigationBarTopView addSubview:label];

    UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    [_navigationBarTopView addGestureRecognizer:singleFingerTap];

    [self.navigationController.view addSubview:_navigationBarTopView];





-(float) statusBarHeight

CGSize statusBarSize = [[UIApplication sharedApplication] statusBarFrame].size;
return MIN(statusBarSize.width, statusBarSize.height);


-(void)handleSingleTap:(UITapGestureRecognizer*)recognizer
NSLog(@"recognizer == %@", recognizer.view.tag);

enter image description here

以上是关于在导航栏中添加自定义视图,如whatsapp的主要内容,如果未能解决你的问题,请参考以下文章

Wordpress 导航栏中的自定义简码

如何创建自定义标签栏...在标签栏中添加自定义图像(无需 xib 更改)

单击导航栏中的“返回”按钮时如何设置自定义视图控制器?

如何在 swift 中使用自定义导航栏制作三个分页视图

如何为整个应用自定义 UINavigationBar?

导航栏中的 Swift 自定义后退按钮