![](/img/trans.png)
[英]iOS:How to dismiss other UIViewcontroller in current UIViewcontroller
[英]How to show UIViewController in other UIViewController?
這是一個典型的問題,可能會重復,但是。
有iPad應用程序具有UINavigationBar
和UITabBar
。 我在navigationBar上創建了一個按鈕,該按鈕必須顯示appinfoViewController 。
當我展示它時,navigationBar和tabTab仍然可以點擊。 但我想將appinfoViewController顯示為完整應用的主屏幕尺寸(如模式)
這是我的代碼:
- (void)showInfo
{
AboutViewController *about = [[AboutViewController alloc] init];
[about.view setFrame: self.view.frame];
[about.view setAlpha:0.0];
[UIView animateWithDuration:0.5
delay:0.0
options: UIViewAnimationOptionCurveLinear
animations:^{
[about.view setAlpha:1.0];
[self.view addSubview:about.view];
[self addChildViewController:about];
[about didMoveToParentViewController:self];
}
completion:^(BOOL finished){
}];
}
如何將appinfoViewController呈現為全屏?
可能可以模態顯示但沒有黑色背景嗎?
如評論中所建議,使用
[self presentViewController:about animated:YES completion:nil]
將擺脫導航欄和標簽欄。 如果需要保留它們,則需要使用
[self.navigationController pushViewController:about animated:YES];
編輯:
為了使除About視圖之外的所有位置都禁用用戶交互,這有點棘手:首先,您需要將所有UI元素嵌入到不是當前視圖控制器主視圖的視圖中。
假設您只有一個按鈕(“顯示有關”按鈕),您不僅會將其放置在主視圖中,而且會使用與該視圖一樣大的另一個視圖(我們稱其為“外部視圖”)。查看控制器的視圖以及放置按鈕的位置(以及您可能擁有的任何其他ui元素)。 您還需要此外觀的出口。 然后編寫如下方法:
-(void)userInteractionEnabled:(BOOL)enabled
{
self.navigationController.navigationBar.userInteractionEnabled = enabled;
self.tabBarController.tabBar.userInteractionEnabled = enabled;
self.outerView.userInteractionEnabled = enabled;
}
另外,您可以簡單地禁用每個“交互式”插座,而不是outerView。 因此,例如,如果您有2個文本視圖,3個按鈕和1個UIPickerView,則可以為每個出口設置userInteractionEnabled = enabled
(而不是僅對父視圖進行設置)。
現在,在showInfo方法中,您可以擁有:
CGRect frame = CGRectMake(50, 50, 200, 200); //Use whatever origin and size you need
about.view.frame = frame;
[self.view addSubview:about.view];
[self userInteractionEnabled:NO]
在您的btnClose方法中,您可以輸入:
[about.view removeFromSuperview];
[self userInteractionEnabled:YES];
希望對您有所幫助,讓我知道這是否是您所需要的!
PS也許您已經意識到了這一點,但是有一個類UIPopoverController
,僅適用於iPad的應用程序,幾乎可以為您完成所有這些工作。 您可以在此處找到有關如何使用它的教程。
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
AboutViewController *about = (AboutViewController *)[storyBoard instantiateViewControllerWithIdentifier:@"about"];
[self.navigationController pushViewController:about動畫:是];
您可以將viewcontroller視圖層直接添加到呈現viewcontroller視圖。
代碼應該像-
AboutViewController *about = [[AboutViewController alloc] init];
[about.view setFrame: self.view.bound];
[about.view setAlpha:0.0];
[self.view addSubview:about.view];
[UIView animateWithDuration:0.5
delay:0.0
options: UIViewAnimationOptionCurveLinear
animations:^{
[about.view setAlpha:1.0];
}
completion:^(BOOL finished){
}];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.