2012-04-26 68 views
0

我不知道如何实现,但下面的图片显示了我想要显示模式的大小:调整大小模态的视图

Modal View Controller

我用来推模式视图代码:

CustomerLogin *customerlogin = [[CustomerLogin alloc] initWithNibName:@"CustomerLogin" bundle:nil]; 
     [self presentModalViewController:customerlogin animated:YES]; 

     [customerlogin release]; 

编辑---

低于建议做出改变这个样子:

UIView *overlay = [[UIView alloc] initWithFrame:self.view.bounds]; 
     overlay.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5]; // Black color 
     [self.navigationController.view addSubview:overlay]; 

     //Add Login View 
     CustomerLogin *loginView = [[CustomerLogin alloc] initWithFrame:CGRectMake(0,0, 200, 300)]; //I guessed a size 
     loginView.center = overlay.center; //Center the view 
     [overlay addSubview:loginView]; 

获得3个错误:

物业中心不处于CustomerLogin 存在CustomerLogin不会响应initWithFrame 不兼容的指针类型发送CustomerLogin *键入的UIView *

+0

向我展示您的CustomerLogin视图/ xib屏幕,对象必须位于中心。背景颜色是黑色的alpha = 0.3 – vishy 2012-04-26 13:21:36

+1

它不起作用,因为你的客户登录是UIViewController的子类,对吧?如果是的话,你必须使用Jason McTaggart代码,但这不是最好的方法。 – bontoJR 2012-04-27 08:02:35

回答

1

使用

-(void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
     // grab an image of our parent view 
    UIView *parentView = self.presentingViewController.view; 

     // For iOS 5 you need to use presentingViewController: 
     // UIView *parentView = self.presentingViewController.view; 

    UIGraphicsBeginImageContext(parentView.bounds.size); 
    [parentView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *parentViewImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

     // insert an image view with a picture of the parent view at the back of our view's subview stack... 
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds]; 
    imageView.image = parentViewImage; 
    [imageView setHidden:YES]; 
    [self.view insertSubview:imageView atIndex:0]; 
} 
-(void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 
    [[self.view.subviews objectAtIndex:0] setHidden:NO]; 
} 

- (void)viewWillDisappear:(BOOL)animated { 
    [super viewWillDisappear:animated]; 

     // remove our image view containing a picture of the parent view at the back of our view's subview stack... 
    [[self.view.subviews objectAtIndex:0] removeFromSuperview]; 
} 

在您的模态视图控制器中

请记住您的模态视图需要充满屏幕,以便把你想成为的模式是什么争夺透明背景

另一个全屏幕大小视图内模态视图控制器的观点,将需要这种观点

+0

尽管如此,我希望使背景不能完全透明。我想要像0.5 alpha一样放置另一个黑色背景,我怎么能做到这一点? – Bradley 2012-04-27 09:58:11

2

你试图提出一个模式视图控制器,而不是一个视图。

完成此操作的最佳方法是创建叠加层(例如,使用alpha 0.5的黑色),然后在其上显示登录视图。

覆盖用于防止用户与背景中的控件交互(如果使用NavigationViewController,按钮,选项卡等,则返回按钮)。

这是一个例子:

UIView *overlay = [[UIView alloc] initWithFrame:self.view.bounds]; 
overlay.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5]; // Black color 
[self.view addSubview:overlay]; 

//Add Login View 
CustomLoginView *loginView = [[CustomLoginView alloc] initWithFrame:CGRectMake(0,0, 200, 300)]; //I guessed a size 
loginView.center = overlay.center; //Center the view 
[overlay addSubview:loginView]; 

请记住,如果你有一个TabBarController或NavigationController应覆盖适用于它,而不是使用[self.view addSubview:overlay]使用[self.tabBarController.view addSubview:overlay][self.navigationController.view addSubview:overlay]