繁体   English   中英

如何将一个ViewController或View加载到具有透明背景的另一个ViewController中?

[英]How can I load a ViewController or View into another ViewController with transparent background?

在我的应用程序中,我希望从UIButton调用时出现一个自定义弹出窗口。 当前,我正在使用TSAlertView,但我想实现更多的UI项并具有更多的自定义版本。 因此,我想做的是加载一个视图控制器或另一个视图控制器中的视图,并能够在它们之间传递信息。 请参阅以下示例:

视图控制器

根据我所读的内容,在另一个视图控制器中加载另一个视图控制器应该不是一个好习惯。 在这种情况下,我认为uiview是推荐的方法。 这个对吗?

我希望弹出窗口后面的viewcontroller外观变暗,也许是深色,不透明度设置为70%。 我该如何实现?

本质上,我正在尝试构建外观类似于AlertView的东西。

如果打算完全阻止背景视图,则可以将viewControllerB设置为Form Sheet

准备显示viewControllerB ,请使用:

[viewControllerA presentViewController:viewControllerB animated:TRUE completion:nil]

您将需要一种方法来消除viewControllerB 按钮,网络操作等

故事板的一个示例:

- (void)someMethod {
  UIStoryboard *storyboard = self.storyboard;
  ViewControllerB *viewCotrollerB = [storyboard instantiateViewControllerWithIdentifier:@"ViewControllerB"];
  [self presentModalViewController:viewControllerB animated:YES];
}

在此处输入图片说明

嗨,SReca,我也有相同的要求(如上图所示)来创建像UIalertview这样的视图。

在这里,您将需要创建一个新的UIView类而不是UIViewController,而无需.xib。

在新的UIView的init方法类中,您将需要以编程方式创建用户界面。 并尽可能保持其高度宽度。

为了加载这个新的UIView,您将只需要创建该uiview的对象并调用initwithframe,它将像uialerview一样以编程方式呈现该视图。

这是参考代码。

ModalPreviewViewController_iPhone.h

@interface ModalPreviewViewController_iPhone : UIView
@end

ModalPreviewViewController_iPhone.m

只需根据需要在initWithFrame中修改此示例代码

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        popUpView  = [[UIView alloc] initWithFrame:CGRectMake(30,30,260,420)];

        popUpBgView = [[UIView alloc] init];
        popUpBgView.frame =CGRectMake(0,0,260,420);
        [popUpBgView setBackgroundColor:[UIColor colorWithRed:51.0/255 green:51.0/255 blue:51.0/255 alpha:1]];
        [popUpView addSubview:popUpBgView];


        imageGalleryView=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 260, 360)];
        [imageGalleryView setBackgroundColor:[UIColor redColor]];

        CGRect btnTempFrame=CGRectMake(5, 385, 90, 30);
        btnTemp=[[UIButton alloc]initWithFrame:btnTempFrame];
        [btnTemp setBackgroundImage:[UIImage imageNamed:@"btn_plain_iphone.png"] forState:UIControlStateNormal];
        [btnTemp.titleLabel setFont:[UIFont boldSystemFontOfSize:13]];
        [btnTemp.titleLabel setTextColor:[UIColor whiteColor]];
        [btnTemp addTarget:self action:@selector(onDownloadClick:) forControlEvents:UIControlEventTouchUpInside];

        CGRect btnSubscribeFrame=CGRectMake(100, 385, 90, 30);
        UIButton *btnSubscribe=[[UIButton alloc]initWithFrame:btnSubscribeFrame];
        [btnSubscribe setBackgroundImage:[UIImage imageNamed:@"btn_plain_iphone.png"] forState:UIControlStateNormal];

        [btnSubscribe.titleLabel setFont:[UIFont boldSystemFontOfSize:13]];
        [btnSubscribe.titleLabel setTextColor:[UIColor whiteColor]];
        [btnSubscribe setTitle:@"Subscribe" forState:UIControlStateNormal];
        [btnSubscribe addTarget:self action:@selector(moveTosubscribe:) forControlEvents:UIControlEventTouchUpInside];

        CGRect closeButtonFrame=CGRectMake(195, 385, 60, 30);
        UIButton *closeButton=[[UIButton alloc]initWithFrame:closeButtonFrame];
        [closeButton setBackgroundImage:[UIImage imageNamed:@"btn_plain_iphone.png"] forState:UIControlStateNormal];
        [closeButton.titleLabel setFont:[UIFont boldSystemFontOfSize:13]];
        [closeButton.titleLabel setTextColor:[UIColor whiteColor]];
        [closeButton setTitle:@"Close" forState:UIControlStateNormal];
        [closeButton addTarget:self action:@selector(closeView:) forControlEvents:UIControlEventTouchUpInside];

        CGRect swipeLabelFrame=CGRectMake(0, 0, 140, 15);
        UILabel *swipeLabel=[[UILabel alloc]initWithFrame:swipeLabelFrame];

        [swipeLabel setBackgroundColor:[UIColor clearColor]];
        [swipeLabel setText:@"> SWIPE TO SEE MORE"];
        [swipeLabel setTextColor:[UIColor lightGrayColor]];
        [swipeLabel setFont:[UIFont systemFontOfSize:12]];

        CGRect dateLabelFrame=CGRectMake(5, 365, 100, 15);
        dateLabel=[[UILabel alloc]initWithFrame:dateLabelFrame];

        [dateLabel setBackgroundColor:[UIColor clearColor]];
        [dateLabel setText:@"July 2013 #93"];
        [dateLabel setTextColor:[UIColor whiteColor]];
        [dateLabel setFont:[UIFont systemFontOfSize:13]];

        CGRect priceLabelFrame=CGRectMake(155, 365, 100, 15);
        priceLabel=[[UILabel alloc]initWithFrame:priceLabelFrame];

        [priceLabel setBackgroundColor:[UIColor clearColor]];
        [priceLabel setText:@"$3.9"];
        [priceLabel setTextColor:[UIColor colorWithRed:71.0/255 green:191.0/255 blue:226.0/255 alpha:1]];
        [priceLabel setFont:[UIFont systemFontOfSize:13]];
        [priceLabel setTextAlignment:NSTextAlignmentRight];



        CGRect labelFrame=swipeLabel.frame;
        labelFrame.origin=CGPointMake(popUpView.frame.origin.x+popUpView.frame.size.width - labelFrame.size.width, popUpView.frame.origin.y - labelFrame.size.height);


        [swipeLabel setFrame:labelFrame];

        [popUpView addSubview:imageGalleryView];
        [popUpView addSubview:btnTemp];
        [popUpView addSubview:closeButton];
        [popUpView addSubview:btnSubscribe];
        [popUpView addSubview:dateLabel];
        [popUpView addSubview:priceLabel];

        [UIView beginAnimations:@"curlup" context:nil];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDuration:.5];
        [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:popUpView cache:YES];
        [self addSubview:popUpView];

        [self addSubview:swipeLabel];
        [UIView commitAnimations];

        UIColor *color = [[UIColor alloc] initWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
        self.backgroundColor = color;

        [[[UIApplication sharedApplication] keyWindow] addSubview:self];
        [self initializeVariables];
    }
    return self;
}

在父视图控制器中,只需创建UIView类的对象,然后使用所需的initWithFrame和大小调用它即可

ModalPreviewViewController_iPhone *mpvc=[ModalPreviewViewController_iPhone alloc];

        if(appDelegate.isIphone5)
            mpvc=[mpvc initWithFrame:CGRectMake(0,0,320,480)];
        else
            mpvc=[mpvc initWithFrame:CGRectMake(0,0,320,568)];

和解雇UivewClass

在它的(UIviewClass的)方法之一(如closeView)中写下以下句子以消除它

[self removeFromSuperview];

如果您遇到任何问题,请告诉我

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM