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

Ios 如何将ViewController或视图加载到另一个具有透明背景的ViewController中?,ios,xcode,uiviewcontroller,popup,subview,Ios,Xcode,Uiviewcontroller,Popup,Subview,在我的应用程序中,我想有一个自定义弹出窗口,当从UIButton调用时显示。目前我正在使用TSAlertView,但我想实现更多的UI项,并有一个更自定义的构建。所以我想做的是在另一个viewcontroller中加载一个viewcontroller或视图,并能够在它们之间传递信息。请参见下面的示例: 据我所知,在另一个viewcontroller中加载另一个viewcontroller可能不是一个好的做法。在这种情况下,我假设uiview是推荐的方式。这是正确的吗 我希望弹出窗口后面的vie

在我的应用程序中,我想有一个自定义弹出窗口,当从UIButton调用时显示。目前我正在使用TSAlertView,但我想实现更多的UI项,并有一个更自定义的构建。所以我想做的是在另一个viewcontroller中加载一个viewcontroller或视图,并能够在它们之间传递信息。请参见下面的示例:

据我所知,在另一个viewcontroller中加载另一个viewcontroller可能不是一个好的做法。在这种情况下,我假设uiview是推荐的方式。这是正确的吗

我希望弹出窗口后面的viewcontroller具有暗淡的外观,可能是一种暗颜色,不透明度设置为70%。我怎样才能做到这一点


基本上,我正在尝试构建类似于AlertView的东西,并具有另一个视图的外观。

如果要完全阻止背景视图,可以将
viewControllerB
设置为
表单

准备好显示
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的类In 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和size调用它

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];

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

谢谢您的快速回复。我还在学习iOS开发中的新东西,那么如何在iphone情节提要中将viewController设置为表单呢?通过在viewController B中将视图设置为clear(清除),我可以看到viewController A是否正确?@SReca,我认为您必须将该视图排除在情节提要之外,并在distinct.xib文件和关联的视图控制器中创建。若要将.xib或nib添加到项目中,我需要执行“新建文件->iOS->用户界面->视图”@SReca“新建视图控制器”。将有一个包含.xib文件的选项。我能够找出您需要的内容,并成功加载了另一个视图。虽然加载后,它后面的viewcontroller变为黑色。在做了进一步的研究之后,我发现viewController A被移除了。那么我需要在容器控制器中添加A和B吗?这是iphone还是ipad?