[英]Add `UIView` above `UIViewController`
在我的應用程序中,我想做一些類似於Spotify在其應用程序中所做的事情。
他們在應用程序中所有其他視圖上方添加了一個UIView
(紅色條)。 我嘗試通過將以下代碼添加到RootSplitViewController
來做到這RootSplitViewController
,但這不起作用。
- (void)viewWillAppear:(BOOL)animated {
[self.view addSubview:[[BannerView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44.0f)]];
for (int i = 0; i < [self.viewControllers count]; i++) {
UIViewController *vc = [self.viewControllers objectAtIndex:i];
vc.view.frame = CGRectMake(vc.view.frame.origin.x, 44, vc.view.frame.size.width, vc.view.frame.size.height-44);
}
}
這只是在我的UINavigationBar
之上添加了一個紅色視圖,但沒有“按下它”。 關於如何解決這個問題的任何想法?
您可以將視圖作為子視圖添加到UIApplication.shared.keyWindow
例如您的情況:
在Swift 4中
let window = UIApplication.shared.keyWindow!
let fullScreenView = UIView(frame: CGRect(x: window.frame.origin.x, y: window.frame.origin.y, width: window.frame.width, height: window.frame.height))
let yourRedView = UIView(frame: CGRect(x: 0, y: 0, width: fullScreenView.frame.width, height: 100))
fullScreenView.backgroundColor = UIColor.gray
yourRedView.backgroundColor = UIColor.red
fullScreenView.addSubview(yourRedView)
window.addSubview(fullScreenView)
在Objective-C中
UIWindow* window = [UIApplication sharedApplication].keyWindow;
UIView* fullScreenView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, window.frame.size.width, window.frame.size.height)];
UIView* yourRedView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, window.frame.size.width, 100)];
[yourRedView setBackgroundColor:[UIColor redColor]];
[fullScreenView setBackgroundColor:[UIColor grayColor]];
[fullScreenView addSubview:yourRedView];
[window addSubview:fullScreenView];
我將在您的層次結構的根部創建一個containerViewController。 它有兩個子ViewController:您當前的rootVC和保存紅色視圖的VC。 現在,每次顯示紅色視圖時,containerVC都可以更改子視圖控制器的邊界,並在屏幕上顯示它們,甚至帶有一些動畫。
另請參閱此處的Apple文檔: https : //developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/ImplementingaContainerViewController.html
您可以在窗口級別添加視圖。
let appDelegate = UIApplication.shared.delegate as! AppDelegate
if let win = appDelegate.window {
win.addSubview(<#Your View#>)
}
這會將視圖添加為UIWindow
子視圖。
目標C
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[[appDelegate window] addSubView:<#Your View#>];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.