【问题标题】:Custom UI: Popup in iOS Objective-C自定义 UI:iOS Objective-C 中的弹出窗口
【发布时间】:2016-12-23 08:50:36
【问题描述】:

我想创建一个自定义弹出窗口,并且我想在不使用 UI 拖放的情况下动态生成代码。

视图应如下所示:

我正在使用以下代码来调用弹出窗口:

 CustomPopUp *cp = [[CustomPopUp alloc]init];
 cp.view.frame = CGRectMake(0.0, 0.0, 300, 300);
 KLCPopup* popup = [KLCPopup popupWithContentView:cp.view];
 [popup show];

自定义弹出代码如下:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.gaScreenName = @"Login";
    if (firstPageLoad) {          
        return;
    }
    LinearLayoutPageHeading *heading = [[LinearLayoutPageHeading alloc] initWithText:@"Sign in." maxWidth:[LayoutValues getMaxWidthClipped]];

    [self.topContainerContent addObject:heading];
    NSString *content = @"New patient? Register here.";

    LinearLayoutLinksViewItem *contentItem = [[LinearLayoutLinksViewItem alloc] initLinksViewWithText:content font:[PPFonts genericParagraphFont] maxWidth:[LayoutValues getMaxWidthClipped] links:nil];
    [self.topContainerContent addObject:contentItem];
    LinearLayoutTextInputAndLabel *emailField = [[LinearLayoutTextInputAndLabel alloc] initWithLabelText:@"EMAIL ADDRESS" maxWidth:[LayoutValues getMaxWidthClipped]];
    emailField.textField.autocapitalizationType = UITextAutocapitalizationTypeNone;        
    LinearLayoutButton *continueButton = [[LinearLayoutButton alloc] initWithText:@"SIGN IN" maxWidth:[LayoutValues getMaxWidthClipped] url:nil type:@"primary"];
    [self.topContainerContent addObject:continueButton];
    LinearLayoutLinksViewItem *noticesPar = [[LinearLayoutLinksViewItem alloc] initLinksViewWithText:@"By clicking Sign In you agree to our Consent to Telehealth, Consent to Request Medical Services, Privacy Policy and Terms of Use." font:[PPFonts signInTermsParagraph] maxWidth:[LayoutValues getMaxWidthClipped] links:nil];
    noticesPar.padding = CSLinearLayoutMakePadding(0, noticesPar.padding.left, 10, noticesPar.padding.right);
    [self.topContainerContent addObject:noticesPar];
    [self renderPageContainers];
    firstPageLoad = YES;
}
@end

我正在使用自定义创建的视图控制器,但弹出一个空白。

有没有办法使用标准UIViewController 获得上述布局?我是 iOS 开发新手,目前还没有动态生成 UI 视图。

【问题讨论】:

    标签: ios objective-c uiviewcontroller popup


    【解决方案1】:

    如果CustomPopUp是一个viewcontroller,你需要用storyboard vc或者xib来实例化它,像这样:

    //For xib:
    let a = UIViewController(nibName: "CustomPopUp", bundle: NSBundle.mainBundle()) as! CustomPopUp
    
    //For storyboard
    let b = UIStoryboard(name: "main", bundle: NSBundle.mainBundle()).instantiateViewControllerWithIdentifier("CustomPopup") as! CustomPopUp
    

    不完全确定您是如何处理这个问题的,但我建议您创建自定义视图,而不是自定义视图控制器,更易于创建和使用

    【讨论】:

    • 谢谢你会尝试让你知道:)
    【解决方案2】:

    使用 xib 创建自定义视图并添加当前视图控制器的子视图。

    制作popupView的属性实例

    在您的 viewController.h

    @property (strong,nonatomic) YourPopUpViewClass *popupView; 
    

    在您的 viewController.m

    设置弹出视图

      self.popUpView.hidden = YES;
      self.popUpView.frame = [UIScreen mainScreen].bounds;
      self.popUpView.alpha = 0.0;
      self.popUpView.delegate = self; // for hide PopupView and Other Functinality if you need
     [self.view addSubview:self.popUpView];
    

    现在显示按钮点击动画

    -(void)showPopupView{
    
       self.popUpView.hidden = No;
        [UIView animateWithDuration:0.0 delay:0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^ {
                          self.popUpView.alpha = 0.0
                     }completion:^(BOOL finished) {
    
                         [UIView animateWithDuration:0.3
                                               delay:0
                                             options:UIViewAnimationOptionCurveEaseInOut
                                          animations:^ {
                                              self.popUpView.alpha = 1.0
                                          }completion:^(BOOL finished) {
    
                                          }];
    
                     }];
    }
    

    然后像这样隐藏

    -(void)hidePopupView {
        self.popUpView.hidden = No;
        [UIView animateWithDuration:0.3 delay:0
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^ {
                             self.popUpView.alpha = 0.0
                         }completion:^(BOOL finished) {
                             self.popUpView.hidden = Yes;
                         }];
    }
    

    希望这会对你有所帮助。

    【讨论】:

    • 我申请了。你必须删除这一行 --> self.popUpView.frame = [UIScreen mainScreen].bounds; .否则这将占据整个屏幕。然后我们的视图将出现,但这段代码确实使背景透明。如果你能弄清楚然后告诉我怎么做?
    • 我只是从一个视图推送另一个视图。在此代码之后,推送的视图将显示为透明。我之前用过很多次。就这么简单。 --> 设置 *set = [[settings alloc] initWithNibName:@"settings" bundle:nil]; --> set.view.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.3f]; --> [self.navigationController pushViewController:set animated:YES ]; .... 但是使用最新的 ios 和 xcode,推送视图不再透明。因此,如果您对此有任何其他解决方案,请告诉我。
    • 我之前看过。然后我也提出了这个问题。调查一下。这个[stackoverflow.com/questions/44803428/…]。你会明白的,我在找什么。
    【解决方案3】:

    这是自定义弹出窗口的好例子。 Custom PopUP View。只需在其中添加您的容器即可。

    【讨论】:

      【解决方案4】:

      找到了可行的解决方案。我使用第三方库实现了结果KLCPopup

      但是我有一个新问题。我的弹出窗口确实有一个自定义视图,但它来自一个单独的类(视图)。如何根据弹出视图的输入更改当前视图?我可以链接到一些有用的文档吗?我不需要代码,只需要指出正确的方向。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-12
        • 2021-02-24
        • 1970-01-01
        • 2011-05-08
        相关资源
        最近更新 更多