0

我正在开发一个应用程序,并且有大量的弹出窗口。从子视图、导航栏项目等的按钮中显示出来。但它们必须始终出现在屏幕的中心。所以我对呈现的矩形进行了硬编码,并让它们出现在中心。这都是纵向模式。现在我也必须让应用程序在景观中工作。因为坐标是手工编码的。它只是不工作。我怎么解决这个问题 ?谢谢

4

1 回答 1

0

我最后写了下课。基本上所有的弹出框都是 560,545 大小,所以我对其进行了硬编码。

AnimatedViewController.h

#import <UIKit/UIKit.h>

/*
 Animated popover class.

 This class intherits from the UIPopover controller class to add following 
 functionality

 * Add a full screen translucent black bacground
 * Popover appears to be sliding up from bottom of the screen
 * Presents the viewController used to initialize the popover controller
   in the center of the screen
 * Always presents the Popover from the topViewController.view and never from a 
   subview
 * Register to orientation change notification.

 Also see @KSCustomPopoverBackgroundView for formatting of the view controller.

 */

@interface AnimatedPopoverController : UIPopoverController{
    UIView* blackTranslucentBackGround;
    UIView* animatingView;
    UIView* mainView;
}


- (id)initWithViewControllerContent:(UIViewController*)viewController;

-(void)presentPopover;

@end

AnimatedViewController.m

#import "AnimatedPopoverController.h"
#import "Utilities.h"

@implementation AnimatedPopoverController

- (id)initWithViewControllerContent:(UIViewController*)viewController{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
    self = [super initWithContentViewController:viewController];
    if (self){
        CGSize fullScreenSize = [[UIScreen mainScreen] bounds].size;
        [viewController.view setBackgroundColor:[UIColor clearColor]];
        mainView = viewController.view;

        // Create the center view
        animatingView = [[UIView alloc] initWithFrame:CGRectMake(0,
                                                                 0,
                                                                 560,
                                                                 545)];
        for (UIView* subview in [viewController.view subviews]){
            [animatingView addSubview:subview];
        }
        [viewController.view addSubview:animatingView];
        // Create black backdrop
        blackTranslucentBackGround = [[UIView alloc]
                                      initWithFrame:CGRectMake(0,0,fullScreenSize.width,fullScreenSize.height)];

        [blackTranslucentBackGround setAlpha:0.5];
        [blackTranslucentBackGround setBackgroundColor:[UIColor blackColor]];
        [viewController.view addSubview:blackTranslucentBackGround];
        [viewController.view sendSubviewToBack:blackTranslucentBackGround];
        [self setPopoverLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
        [self setPopoverBackgroundViewClass:[KSCustomPopoverBackgroundView class]];

    }
    return self;
}

- (void) presentPopover{
    [self centerViewForOrienation];
    [self slideAnimation:animatingView];
    UIViewController* topViewController = [Utilities topMostController];
    [self presentPopoverFromRect:topViewController.view.frame
                          inView:topViewController.view
        permittedArrowDirections:0
                        animated:YES];
}

-(void) slideAnimation:(UIView*) view{
    [view setCenter:CGPointMake(view.center.x,view.center.y+300)];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.15];
    view.center = CGPointMake(view.center.x,view.center.y-300);
    [UIView commitAnimations];
}


-(void) didRotate:(NSNotification*)notification{
    [self centerViewForOrienation];
}

-(void)centerViewForOrienation{
    UIInterfaceOrientation  interfaceOrientation =   [[UIApplication sharedApplication] statusBarOrientation];
    CGSize fullScreenSize = [[UIScreen mainScreen] bounds].size;

    if(interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown ||
       interfaceOrientation == UIInterfaceOrientationPortrait) {
        [animatingView setCenter:CGPointMake(fullScreenSize.width/2,
                                             fullScreenSize.height/2)];

        [self setPopoverContentSize:fullScreenSize];
        [blackTranslucentBackGround setFrame:CGRectMake(0,0,fullScreenSize.width,fullScreenSize.height)];
    }else{
        [animatingView setCenter:CGPointMake(fullScreenSize.height/2,
                                             animatingView.frame.size.height/2 + 20)];

        [self setPopoverContentSize:CGSizeMake(fullScreenSize.height, fullScreenSize.width)];
        [blackTranslucentBackGround setFrame:CGRectMake(0,0,fullScreenSize.height,fullScreenSize.width)];
    }
}


@end

topmostview控制器的实用方法

+ (UIViewController*) topMostController
{
    UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;

    while (topController.presentedViewController) {
        topController = topController.presentedViewController;
    }

    return topController;
}

用法:我在故事板中创建了一个弹出视图控制器并将其发送,然后使用如下类:

 popOverViewController = [[AnimatedPopoverController alloc] initWithViewControllerContent:changePhotoViewController];
    [popOverViewController presentPopover];
于 2013-04-11T09:21:44.553 回答