我正在开发一个应用程序并且有大量的弹出窗口。从子视图,导航栏项目等按钮显示。但它们必须始终出现在屏幕的中心。所以我对呈现矩形进行了硬编码,让它们出现在中心。这一切都处于纵向模式。现在我必须使应用程序在Landscape中运行。 Sicne的坐标是手动编码的。它只是不起作用。我怎么解决这个问题 ?谢谢
答案 0 :(得分:0)
我最后写了下课。基本上所有的popover都是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
最顶层视图控制器的实用方法
+ (UIViewController*) topMostController
{
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
return topController;
}
用法:我在故事板中创建一个弹出视图控制器并发送它,然后按如下方式使用该类:
popOverViewController = [[AnimatedPopoverController alloc] initWithViewControllerContent:changePhotoViewController];
[popOverViewController presentPopover];