簡體   English   中英

Xcode:如何創建出現在另一個視圖控制器中的PopUp視圖控制器

[英]Xcode: How To Create A PopUp View Controller That Appears In Another View Controller

基本上我想要弄清楚的是,我說有一個名為V1的視圖控制器,里面有一個常規視圖和一個按鈕。 現在,當您點擊該按鈕時,我希望該按鈕創建一個動作,在同一視圖控制器V1中彈出另一個名為V2的視圖控制器。

V2的大小會減小一些,以至於它不會填滿整個屏幕,但你仍然可以看到V2后面的第一層是V1。 所以基本上,你永遠不會離開V1。 我希望這對我正在嘗試做的事情有意義。 我知道MTV應用程序具有這種功能。 我正在談論的圖片如下: https//docs.google.com/leaf?id = 0BzlCAVXRsIPcNWUxODM2MDAtNDE3OS00ZTc4LTk5N2MtZDA3NjFlM2IzNmZk&hl = en_US

示例代碼或示例也是我正在尋找的。

謝謝

您可以通過設置modalPresentationStyle相應屬性類型來創建此類視圖。 請參閱下面的示例:

UIViewController *V2 = [[UIViewController alloc] init];
V2.modalPresentationStyle = UIModalPresentationFormSheet;
V2.modalTransitionStyle = UIModalTransitionStyleCoverVertical;     
[V1 presentViewController:V2 animated:YES completion:nil];
V2.view.superview.frame = CGRectMake(0, 0, 540, 620); //it's important to do this after presentModalViewController
V2.view.superview.center = V1.view.center;
[V1 release];

嘗試這個:

V2 *d = [[V2 alloc]initWithNibName:@"V2" bundle:nil];//assuming V2 is name of your nib as well
d.delegate = self; //Optional:you only need this if you want to delegate

 //create popover and put V2 in the popover view
UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:d]; 
popoverController.delegate = self;  //optional
CGSize size = CGSizeMake(325, 75); // size of view in popover…V2
popoverController.popoverContentSize = size;
[d release];
[popoverController presentPopoverFromRect:yourButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

如果你想在iOS 8中將其作為模態彈出窗口呈現,其風格類似於OP的截圖,這就是我所做的:

UIViewController *V2 = [[UIViewController alloc] init];  // V2 is the popup
V2.modalPresentationStyle = UIModalPresentationFormSheet;
V2.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
V2.preferredContentSize = CGSizeMake(325, 75); // size of popup view
[V1 presentModalViewController:V2 animated:YES]; // V1 is the current topmost view controller

我比使用UIPopover更喜歡這個,因為你不需要弄亂箭頭方向,用戶無法通過點擊彈出窗口來關閉它。

這些屬性也可以通過設計器在storyboard / nib中設置。 要設置preferredContentSize,請選中“使用首選顯式大小”並設置值。

這僅適用於iPad。

有一個非常好的庫來顯示視圖控制器,因為iPhone上的Popup請看這里https://github.com/martinjuhasz/MJPopupViewController

如果您使用的是Storyboard,則可以按照以下步驟操作:

  1. 添加視圖控制器(V2),按照您希望的方式設置UI

*基於您附加的圖像

  • 添加UIView - 將背景設置為黑色,將不透明度設置為0.5
  • 添加一個UIImageView - 它將作為你的彈出窗口(請注意,圖像和視圖不能具有相同的級別/層次結構。不要使imageview成為視圖的子視圖,否則uiview的不透明度將影響uiImageView)
  1. 禮物V2模態

  2. 單擊segue。 在“屬性”檢查器中,將“演示文稿”設置為“全屏幕” 如果你願意,刪除動畫

故事板

  1. 選擇V2。 在“屬性”檢查器中,將“演示文稿”設置為“全屏幕” 檢查定義上下文並提供上下文

故事板

  1. 選擇V2的MainView(請檢查圖像)。 將backgroundColor設置為Clear Color

故事板

v2創建UIView並在v1添加。

- (void)viewDidLoad
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self 
                   action:@selector(aMethod:)
         forControlEvents:UIControlEventTouchDown];
    [button setTitle:@"Show View" forState:UIControlStateNormal];
        button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
    [self.view addSubview:button];
}

- (void)aMethod:(id)sender 
{
    CGRect * imageFrame = CGRectMake(10, 90, 300, 300);
    V2 *v2 = [[V2 alloc] initWithFrame:imageFrame];
    [self.view addSubview:v2];
}

file .m --->這是實現文件

-(IBAction)anyAlert:(id)sender{

   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"A Message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK!", @"Other Title", nil];
    [alert show];
    [alert release];
}

記得申報

-(IBAction)anyAlert:(id)sender; 

file .h --->頭文件中

它對我有用,希望對你有用......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM