【问题标题】:Resizing a modal view调整模态视图的大小
【发布时间】:2012-04-26 13:17:11
【问题描述】:

我不确定如何实现它,但下图显示了我想要出现的模态的大小:

我用来推送模态视图的代码是:

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* 的不兼容指针类型

【问题讨论】:

  • 显示您的 CustomerLogin 视图/xib 屏幕,对象必须位于中心。背景颜色为黑色,alpha=0.3
  • 它不起作用,因为您的客户登录是 UIViewController 的子类,对吧?如果是,您必须使用 Jason McTaggart 代码,但这不是最好的方法。

标签: iphone objective-c ios modal-dialog modalviewcontroller


【解决方案1】:

您正在尝试呈现模态视图控制器而不是单个视图。

实现这一点的最佳方法是创建一个叠加层(例如黑色,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]

【讨论】:

    【解决方案2】:

    使用

        -(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.5 alpha 我怎么能这样做?
    【解决方案3】:

    您可能想查看 TSAlertView。它允许您创建自定义视图,这些视图类似于 UIAlertView 的代码。 https://github.com/TomSwift/TSAlertView

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-18
      • 1970-01-01
      • 2013-09-29
      • 1970-01-01
      • 2011-05-15
      相关资源
      最近更新 更多