Добавить кнопку закрытия в угол UIModalPresentationPageSheet - PullRequest
5 голосов
/ 08 августа 2011

Есть ли способ добавить кнопку в углу UIModalPresentationPageSheet? Я имею в виду, что я хочу поместить эту Apple-подобную (x) кнопку в углу страницы листа, но добавив ее в родительский вид, она появится за листом страницы (а также невозможно нажать) и добавив ее на страницу Лист сделает его скрытым, поскольку он находится вне области просмотра.

Есть ли решение?

Спасибо.

Ответы [ 3 ]

1 голос
/ 09 августа 2011

Вот решение, которое я использую.Это не совсем то, что вы описываете, что тоже было бы аккуратно, но было бы сложно, так как вы бы хотели, чтобы кнопка частично выходила за границы представления (как вы говорите, она должна быть дочерней по отношению к контроллеру представления -просмотр суперпредставления).

Мое решение - поместить кнопку закрытия в область левой кнопки навигационной панели.Я делаю это автоматически с помощью расширения класса UIViewController.Чтобы использовать его, просто вызовите [currentViewController presentAutoModalViewController: modalViewController animated: YES];

@implementation UIViewController (Modal)

- (void) presentAutoModalViewController: (UIViewController *) modalViewController withDismissAction: (SEL) onDismiss animated:(BOOL)animated
{
    UINavigationController* nc = nil;
    if ( NO == [ modalViewController isKindOfClass: [UINavigationController class]] )
    {
        nc = [[[UINavigationController alloc] initWithRootViewController: modalViewController] autorelease];

        [nc setToolbarHidden:YES animated: NO];

        nc.modalPresentationStyle = modalViewController.modalPresentationStyle;

        modalViewController.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop
                                                                                                              target:self 
                                                                                                              action:onDismiss] autorelease];
    }
    else
    {
        nc = (UINavigationController*) modalViewController;

        UIViewController* rootViewController = [nc.viewControllers objectAtIndex: 0];
        rootViewController.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop
                                                                                                              target:self 
                                                                                                              action:onDismiss] autorelease];
    }

    [nc setNavigationBarHidden: NO];
    nc.navigationBar.barStyle = UIBarStyleBlack;
    nc.toolbar.barStyle = self.navigationController.navigationBar.barStyle;

    [self presentModalViewController: nc animated: animated ];
}

- (void) presentAutoModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated
{
    [self presentAutoModalViewController:modalViewController withDismissAction: @selector(autoModalViewControllerDismiss:) animated: animated];
}

- (void) autoModalViewControllerDismiss: (id)sender
{
    [self dismissModalViewControllerAnimated:YES];
}

- (BOOL) isAutoModalViewController
{
    return ( self.navigationController != nil && self.navigationController.parentViewController != nil && self.navigationController.parentViewController.modalViewController == self.navigationController );
}

@end
0 голосов
/ 21 июля 2014

Я нашел предложение @ TomSwift очень полезным.Вот версия, в которой используются устаревшие методы iOS7 и ARC.

@implementation UIViewController (Modal)

- (void)presentAutoModalViewController: (UIViewController *) modalViewController withDismissAction:(SEL) onDismiss animated:(BOOL)animated
{
    UINavigationController* nc = nil;
    if ( NO == [ modalViewController isKindOfClass: [UINavigationController class]] )
    {
        nc = [[UINavigationController alloc] initWithRootViewController: modalViewController];
        [nc setToolbarHidden:YES animated: NO];
        nc.modalPresentationStyle = modalViewController.modalPresentationStyle;

        modalViewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop
                                                                                                              target:self
                                                                                                              action:onDismiss];
    }
    else
    {
        nc = (UINavigationController*) modalViewController;

        UIViewController* rootViewController = [nc.viewControllers objectAtIndex: 0];
        rootViewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop
                                                                                                             target:self
                                                                                                             action:onDismiss];
    }

    [nc setNavigationBarHidden: NO];
    nc.navigationBar.barStyle = UIBarStyleBlack;
    nc.toolbar.barStyle = self.navigationController.navigationBar.barStyle;

    [self presentViewController:nc animated:animated completion:nil];
}

- (void)presentAutoModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated
{
    [self presentAutoModalViewController:modalViewController withDismissAction: @selector(autoModalViewControllerDismiss:) animated: animated];
}

- (void)autoModalViewControllerDismiss: (id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (BOOL)isAutoModalViewController
{
    return ( self.navigationController != nil && self.navigationController.parentViewController != nil && self.navigationController.parentViewController.presentedViewController == self.navigationController );
}

@end

И я называю это как ...

MyController *vc = [[MyController alloc] init];
vc.modalPresentationStyle = UIModalPresentationFormSheet;
vc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;   
[self presentAutoModalViewController:info animated:YES];
0 голосов
/ 08 августа 2011

РЕДАКТИРОВАТЬ: На самом деле, первое, что я бы предложил вам сделать, это использовать другой тип кнопки закрытия. Например, вы можете добавить панель инструментов вверху с помощью кнопки Done.

Если вы все еще хотите использовать плавающий X в стиле Apple, попробуйте установить свойства, чтобы убедиться, что он не будет скрыт или обрезан. Я бы сказал, что лучший способ сделать это - создать UIButton с изображением переднего плана, которое является изображением кнопки в стиле, и фоновым изображением, которое постепенно исчезает от цвета фона / узоров листа страницы до прозрачного фона вокруг кнопка. Он фактически дает вам «плавающую кнопку закрытия», не выходя за границы листа.

...