Wyświetlanie clearColor UIViewController nad UIViewController

Mam UIViewController widok jako subview / modal na górze innego UIViewController widok, na przykład, że subview/modal powinien być przezroczysty i wszelkie komponenty dodane do subview powinny być widoczne. Problem polega na tym, że mam subview pokazuje czarne tło zamiast clearColor. Próbuję zrobić UIView jako clearColor, a nie czarne tło. Czy ktoś wie, co jest z nim nie tak? Wszelkie sugestie doceniam to.

FirstViewController.m

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];

[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:vc animated:NO];  

SecondViewController.m

- (void)viewDidLoad 
{
     [super viewDidLoad];
     self.view.opaque = YES;
     self.view.backgroundColor = [UIColor clearColor];
}

Rozwiązany : naprawiłem problemy. Działa tak dobrze zarówno dla iPhone ' a, jak i iPada. Kontroler widoku modalnego bez czarnego tła tylko clearColor / transparent. Jedyne, co muszę zmienić, to zamienić UIModalPresentationFullScreen na UIModalPresentationCurrentContext. Jakie to proste!

FirstViewController.m

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
vc.view.backgroundColor = [UIColor clearColor];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:vc animated:NO completion:nil];

NOTICE: If you używają modalPresentationStyle Właściwości navigationController:

FirstViewController.m

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
vc.view.backgroundColor = [UIColor clearColor];
self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:vc animated:NO completion:nil];

Uwaga: zła wiadomość jest taka, że powyższe rozwiązanie nie działa na iOS 7. Dobrą wiadomością jest to, że naprawiłem problem dla iOS7! Poprosiłem kogoś o pomoc i oto, co powiedział:

podczas prezentowania kontrolera widoku modalnie system iOS usuwa Kontrolery widoków znajdujące się pod nim z hierarchii widoków na czas wyświetlania. Podczas gdy widok twojego modalnego prezentowany kontroler widoku jest przezroczysty, pod nim nie ma nic poza oknem aplikacji, które jest czarne. system iOS 7 wprowadził nowy styl prezentacji modalnej, UIModalPresentationCustom, który powoduje, że system iOS nie usuwa widoków pod prezentowanym kontrolerem widoku. Jednak, aby skorzystać z tego stylu prezentacji modalnej, musisz podać swój własny delegat przejścia do obsługi prezentacji i odrzucenia animacji. Jest to opisane w dyskusji "niestandardowe przejścia za pomocą kontrolerów widoku" z WWDC 2013 https://developer.apple.com/wwdc/videos/?id=218 który obejmuje również jak zaimplementować własnego delegata przejściowego.

Możesz zobaczyć moje rozwiązanie powyższego problemu w iOS7: https://github.com/hightech/iOS-7-Custom-ModalViewController-Transitions

Author: P.J.Radadiya, 2012-06-27

16 answers

IOS8 +

W iOS8+ możesz teraz użyć nowego Modalpresentationstyle UIModalPresentationOverCurrentContext, aby przedstawić kontroler widoku z przezroczystym tłem:

MyModalViewController *modalViewController = [[MyModalViewController alloc] init];
modalViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;           
[self presentViewController:modalViewController animated:YES completion:nil];    
 144
Author: Brody Robertson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-04 13:02:40

Rozwiązany : naprawiłem problemy. Działa tak dobrze zarówno dla iPhone ' a, jak i iPada. Kontroler widoku modalnego bez czarnego tła tylko clearColor / transparent. Jedyną rzeczą, którą muszę zmienić, to zamieniłem UIModalPresentationFullScreen na UIModalPresentationCurrentContext. Jakie to proste!

FirstViewController.m

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
    vc.view.backgroundColor = [UIColor clearColor];
    self.modalPresentationStyle = UIModalPresentationCurrentContext;
    [self presentViewController:vc animated:NO completion:nil];

NOTICE: Jeśli używasz właściwości modalPresentationStyle navigationController:

FirstViewController.m

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
    vc.view.backgroundColor = [UIColor clearColor];
    self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;
    [self presentViewController:vc animated:NO completion:nil];

Uwaga: zła wiadomość jest taka, że powyższe rozwiązanie nie działa na iOS 7. Dobrą wiadomością jest to, że naprawiłem problem dla iOS7! Poprosiłem kogoś o pomoc i oto co powiedział:

podczas prezentowania kontrolera widoku modalnie system iOS usuwa Kontrolery widoków znajdujące się pod nim z hierarchii widoków na czas wyświetlania. Podczas gdy Widok modalnie prezentowanego kontrolera widoku jest przezroczysty, nie ma nic pod nim oprócz okna aplikacji, które jest czarne. iOS 7 wprowadził nowy styl prezentacji modalnej, UIModalPresentationCustom, który powoduje, że iOS nie usuwa widoków pod prezentowanym kontrolerem widoku. Jednak, aby skorzystać z tego stylu prezentacji modalnej, musisz podać swój własny delegat przejścia do obsługi prezentacji i odrzucenia animacji. Jest to opisane w dyskusji "niestandardowe przejścia za pomocą kontrolerów widoku" z WWDC 2013 https://developer.apple.com/wwdc/videos/?id=218 który obejmuje również jak zaimplementować własnego delegata przejściowego.

Możesz zobaczyć moje rozwiązanie powyższego problemu w iOS7: https://github.com/hightech/iOS-7-Custom-ModalViewController-Transitions

 140
Author: hightech,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-25 02:02:47

Więc dla myślicieli czysto wizualnych i fanów storyboardu, możesz zrobić:

1. Prezentacja Kontrolera Widoku

Zdefiniuj kontekst

2. Prezentowany Kontroler Widoku

Prezentacja: Over Current Context

 114
Author: pasevin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-09-18 23:47:25

Swift 3 & iOS10 rozwiązanie:

//create view controller
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "RoadTripPreviewViewController")
//remove black screen in background
vc.modalPresentationStyle = .overCurrentContext
//add clear color background
vc.view.backgroundColor = UIColor.clear
//present modal
self.present(vc, animated: true, completion: nil)
 25
Author: Kevin ABRIOUX,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-20 13:10:59

To jest z Xcode 7 beta 4 za pomocą sterującego przeciągnięcia segue. Po prostu Ustaw tło miejsca docelowego, aby wyczyścić i ustaw właściwości segue w IB jako to (nb . Prezentacja może być również "na pełnym ekranie"):

Tutaj wpisz opis obrazka

 16
Author: smileBot,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-24 14:47:56

Znalazłem najprostszy sposób, aby uruchomić go na iOS7 i iOS8 jest dodanie ustawienia presentationStyle do UIModalPresentationOverCurrentContext na modallyPresentedVC (ViewController, który chcesz modalnie przedstawić) z powodu iOS8:

[modallyPresentedVC setModalPresentationStyle:UIModalPresentationOverCurrentContext];
[modallyPresentedVC.navigationController setModalPresentationStyle:UIModalPresentationOverCurrentContext];

I UIModalPresentationCurrentContext na presentingVC (kontroler prezentujący modallyPresented) z powodu iOS7:

[presentingVC setModalPresentationStyle:UIModalPresentationCurrentContext];
[presentingVC.navigationController setModalPresentationStyle:UIModalPresentationCurrentContext];

Ponieważ sprawy są obsługiwane inaczej na iOS7 i iOS8. Oczywiście nie musisz ustawiać właściwości navigationController, jeśli ich nie używasz. Mam nadzieję, że to pomoże.

 8
Author: 3vangelos,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-11 11:33:00

Wersja Swift2:

let vc = self.storyboard!.instantiateViewControllerWithIdentifier("YourViewController") as! YourViewController
vc.view.backgroundColor = UIColor.clearColor()
vc.modalPresentationStyle = UIModalPresentationStyle.OverFullScreen // orOverCurrentContext to place under navigation
self.presentViewController(vc, animated: true, completion: nil)
 5
Author: Mojtabye,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-01-03 15:49:39

Inny sposób (nie trzeba tworzyć niestandardowych przejść i działa na iOS 7)

Korzystanie Ze Storyboardu:

Utwórz kontroler widoku potomnego o rozmiarze swobodnym, ustaw szerokość widoku na 500x500 (na przykład) i dodaj następną metodę:

- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    self.view.superview.bounds = CGRectMake(0, 0, 500, 500);
    self.view.superview.backgroundColor = [UIColor clearColor];
}

Następnie utwórz segue modalny z formularzem arkusza i przetestuj go.

 4
Author: educaPix,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-12-11 19:12:45

IOS 7 rozwiązanie z niestandardowym segue:

CustomSegue.h
#import <UIKit/UIKit.h>

    @interface CustomSegue : UIStoryboardSegue <UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning>

    @end



CustomSegue.m
#import "CustomSegue.h"

@implementation CustomSegue

-(void)perform {

    UIViewController* destViewController = (UIViewController*)[self destinationViewController];
    destViewController.view.backgroundColor = [UIColor clearColor];
    [destViewController setTransitioningDelegate:self];
    destViewController.modalPresentationStyle = UIModalPresentationCustom;
    [[self sourceViewController] presentViewController:[self destinationViewController] animated:YES completion:nil];
}


//===================================================================
// - UIViewControllerAnimatedTransitioning
//===================================================================

- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext {
    return 0.25f;
}

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {

    UIView *inView = [transitionContext containerView];
    UIViewController* toVC = (UIViewController*)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIViewController* fromVC = (UIViewController *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];

    [inView addSubview:toVC.view];

    CGRect screenRect = [[UIScreen mainScreen] bounds];
    [toVC.view setFrame:CGRectMake(0, screenRect.size.height, fromVC.view.frame.size.width, fromVC.view.frame.size.height)];

    [UIView animateWithDuration:0.25f
                     animations:^{

                         [toVC.view setFrame:CGRectMake(0, 0, fromVC.view.frame.size.width, fromVC.view.frame.size.height)];
                     }
                     completion:^(BOOL finished) {
                         [transitionContext completeTransition:YES];
                     }];
}


//===================================================================
// - UIViewControllerTransitioningDelegate
//===================================================================

- (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {

    return self;
}

- (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    //I will fix it later.
    //    AnimatedTransitioning *controller = [[AnimatedTransitioning alloc]init];
    //    controller.isPresenting = NO;
    //    return controller;
    return nil;
}

- (id <UIViewControllerInteractiveTransitioning>)interactionControllerForPresentation:(id <UIViewControllerAnimatedTransitioning>)animator {
    return nil;
}

- (id <UIViewControllerInteractiveTransitioning>)interactionControllerForDismissal:(id <UIViewControllerAnimatedTransitioning>)animator {
    return nil;
}

@end

Rozwiązanie oparte na kodzie hightech.

 4
Author: Max Gribov,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-08-10 15:40:18

Dla mnie to działa:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"MMPushNotificationViewController"];

    vc.view.backgroundColor = [UIColor clearColor];
    self.modalPresentationStyle = UIModalPresentationCurrentContext;
#ifdef __IPHONE_8_0
    if(IS_OS_8_OR_LATER)
    {
        self.providesPresentationContextTransitionStyle = YES;
        self.definesPresentationContext = YES;
        [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
    }
#endif


    [self presentViewController:vc animated:NO completion:nil];

MMPushNotificationViewController jest kontrolerem przezroczystego widoku, a także zrobiłem kolor widoku MMPushNotificationViewController jako clearcolor. Teraz wszystko, co zrobiłem i zrobiłem mój Transparentviewcontroller.

 4
Author: Manab Kumar Mal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-12 14:24:39

Dla iOS7

Istnieje teraz sposób, aby to osiągnąć za pomocą niestandardowych przejść iOS7, w ten sposób:

MyController * controller = [MyController new];
[controller setTransitioningDelegate:self.transitionController];
controller.modalPresentationStyle = UIModalPresentationCustom;
[self controller animated:YES completion:nil];

Aby stworzyć własne Przejście, potrzebujesz 2 rzeczy:

  • A TransitionDelegate object (implementing <UIViewControllerTransitionDelegate>)
  • obiekt "AnimatedTransitioning" (implementing <UIViewControllerAnimatedTransitioning>)

Więcej informacji na temat niestandardowych przejść znajdziesz w tym tutorialu.

 2
Author: Kirualex,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-06-20 09:12:55

Działa świetnie na iOS7 i iOS8

UIViewController* vc=[[UIViewController alloc]initWithNibName:@"VC" bundle:nil];

vc.view.alpha=0.7;
[vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];

self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;

[self presentViewController:vc animated:NO completion:nil];
 2
Author: hoppus,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-02-06 12:36:07

Możesz także "ponownie dodać" okno do widoku.

OneViewController *vc = [[OneViewController alloc] init];
if ([self respondsToSelector:@selector(presentViewController:animated:completion:)]) {
    [self presentViewController:vc animated:YES completion:nil];
} else {
    [self presentModalViewController:vc animated:YES];
}

[[[UIApplication sharedApplication] keyWindow] insertSubview:self.view atIndex:0];

I w ten sposób można animować prezentację.

 2
Author: Elf Sundae,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-28 10:25:01

Dla iOS 7 i tylko za pomocą Kreatora interfejsu można to osiągnąć, ustawiając prezentację Na "Over Current Context" na wszystkich kontrolerach widoku zaangażowanych w prezentację modalną. Nawet dla kontrolerów nawigacyjnych.

Na przykład, ustaw go na wszystkich kontrolerach widoku:

NavController -> RootViewController - > ModalViewController

Tutaj wpisz opis obrazka

 1
Author: David Hernandez,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-02-05 22:32:53

Nie bawiłem się zbytnio z Storyboard/Interface builder, ale wyskakuje mi to, że mówisz, że Widok ma być jasny kolor (tj. 100% alpha / see-through), a także mówi, że jest nieprzezroczysty (0% alpha--całkowicie stały). Te dwie rzeczy nie pasują do siebie. Skomentowałbym linię self.view.opaque = YES; i zobaczymy czy wtedy zadziała;)

Ah, coś innego właśnie pomyślałem--jest całkowicie możliwe, że twój kontroler widoku ma tło alfa, ale oczywiście alfa będzie wyświetlanie do koloru okna podstawowego lub kontrolera widoku głównego programu, który jest domyślnie czarny. Sama warstwa podstawowa całej aplikacji nie może mieć przezroczystego tła-przezroczystego do czego? Co za tym stoi? Musi być coś do przejrzenia przez przejrzystość. Czy to ma sens?

 0
Author: WendiKidd,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-28 01:40:31

Po prostu użyj " ja.modalPresentationStyle = UIModalPresentationCurrentContext;", w widoku prezentacji

Będzie działać dobrze:)

 -3
Author: Venkateswarlu.NP,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-14 11:21:33