【问题标题】:Programmatically showing another UIViewController as popup in iOS?在 iOS 中以编程方式将另一个 UIViewController 显示为弹出窗口?
【发布时间】:2016-03-19 19:39:12
【问题描述】:

我有主仪表板 (UITableViewController),一旦我登录后,我需要在此页面上显示一条欢迎消息,该消息使用 UIViewController 显示。

如何通过我的ViewDidAppear() 方法显示此弹出窗口?

我正在使用以下代码,但它不起作用

  -(void)viewDidAppear:(BOOL)animated{
        popupObj= [self.storyboard instantiateViewControllerWithIdentifier:@"popup"];
        [popupObj setModalPresentationStyle:UIModalPresentationCurrentContext];
    }

请帮帮我..

我看到了几个stackoverflow 链接

更新

当我将我的代码更改为这个时

-(void)viewDidAppear:(BOOL)animated{
    popupObj= [self.storyboard instantiateViewControllerWithIdentifier:@"popup"];
   // [popupObj setModalPresentationStyle:UIModalPresentationCurrentContext];
    popupObj.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    popupObj.modalTransitionStyle = UIModalPresentationPopover;
    [self presentViewController:popupObj animated:YES completion:nil];
}

现在我可以看到我的UIViewController 弹出但现在UIViewController 以全屏视图显示。

但我只需要这个frame (320 , 320)

【问题讨论】:

  • 您的链接问题的答案提供了大量代码,您是尝试全部实现还是仅尝试这两行?
  • 是的尝试了与此相关的所有内容
  • 您可以编辑您的问题以包含所有相关代码吗?并且还包括你期望它做什么与它实际做了什么的描述。
  • 你的代码在哪里展示viewcontroller?
  • 现在更新问题

标签: ios objective-c uiviewcontroller popup


【解决方案1】:

您的代码创建视图控制器并设置其呈现样式,但实际上并未呈现它。为此,您需要在您现在拥有的两行之后使用这一行:

[self presentViewController:popupObj animated:true completion:nil];

【讨论】:

    【解决方案2】:
    popupObj= [self.storyboard instantiateViewControllerWithIdentifier:@"popup"];
    popupObj.view.frame = CGRectMake(20, 200, 280, 168);
    [self.view addSubview:popupObj.view];
    [self addChildViewController:popupObj];
    

    您可以将 UIViewController 添加为子视图并将其设置为框架,使其看起来像弹出窗口。 希望这会对你有所帮助。

    【讨论】:

    • 它应该可以工作,改变框架是否只影响 x、y 或不影响它的宽度和高度?
    【解决方案3】:

    请在您的 rootviewcontroller 上设置导航控制器:

     -(void)viewDidAppear:(BOOL)animated
     {  
          popupObj= [self.storyboard instantiateViewControllerWithIdentifier:@"popup"];
          [self.navigationController presentViewController:popupObj animated:YES completion:nil];
    
      }
    

    或者

      -(void)viewDidAppear:(BOOL)animated
      {  
         popupObj= [self.storyboard instantiateViewControllerWithIdentifier:@"popup"];
            [self.view addSubview:popupObj.view];
      }
    

    【讨论】:

      【解决方案4】:

      我有两种方式,可能不太好,但大部分时间都在工作。

      首先,当第二个视图控制器出现时,显示第一个视图控制器的屏幕截图。像这样:

      - (void)setBackGround {
          UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, [UIScreen mainScreen].scale);
          [self.presentingViewController.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:NO];
          UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
          UIGraphicsEndImageContext();
          self.view.layer.contents = (__bridge id)(image.CGImage);
      }
      - (void)viewWillAppear:(BOOL)animated {
          [super viewWillAppear:animated];
          if (!_isShown) {
              _isShown = YES;
              [self setBackGround];
          }
      }
      

      不要忘记在初始化时设置“_isShown = NO”。

      第二:你只能初始化一个视图,并在一个带有动画的视图控制器上显示它。代码在:http://blog.moemiku.com/?p=101

      我在博客上更新了一个示例。 Direct download url

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-20
        • 2018-05-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多