且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在导航栏上添加UIView?

更新时间:2023-11-17 14:15:46

你可以添加应用程序基本视图的子视图

You can add a subview to the base view of the Application

[[[UIApplication sharedApplication] keyWindow] addSubview:vMyCustomUIView];

要确保仅在视图控制器可见时显示,您可以在视图控制器中添加和删除它 viewDidAppear viewDidDisappear 委托方法。下面是一个显示与其重叠的蓝框的示例。

To make sure it is only shown when your view controller is visible you could add and remove it in the viewDidAppear and viewDidDisappear delegate methods. Here is an example that would show a blue box overlapping them.

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.    
    vTestView = [[UIView alloc] initWithFrame:CGRectMake(10.0f,
                                                         10.0f,
                                                        100.0f,
                                                        100.0f)];
    vTestView.backgroundColor = [UIColor blueColor];
}


-(void)viewDidAppear:(BOOL)animated
{
    [[[UIApplication sharedApplication] keyWindow] addSubview:vMyCustomUIView];
}
-(void)viewDidDisappear:(BOOL)animated
{
    [vMyCustomUIView removeFromSuperview];
}