确定viewDidLoad中的框架/边界

各位程序员,

首先,很抱歉,这个post太长了。 我的问题很简单,但我想确保你知道我在做什么,我真的不想改变我的方法的基本思想。

(以下全部以编程方式完成,没有故事板,没有笔尖,没有导航控制器)

我有一个没有自己的看法的RootViewController。 他所做的只是实例化其他ViewController,pipe理其转换并将其视图推送(添加)到主窗口中。 要正确定位这些视图,我想获取其中一个RootViewCOntrollers子控制器的界限/框架。 这就是我如何从appDelegate创buildRootViewController(我从一个空的项目开始)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor grayColor]; self.window.rootViewController = [[UCRootViewController alloc]init]; [self.window makeKeyAndVisible]; return YES; } 

在初始化之后,rootviewController创build一个MapViewController

 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. NSLog(@"RootviewController initialized"); self.mapviewController = [[UCMapviewController alloc] initWithDelegate:self]; self.view = self.mapviewController.view; [self.mapviewController.view setOpaque:YES]; [self.view bringSubviewToFront:self.mapviewController.view]; [self presentModalViewController:self.mapviewController animated:YES]; self.currentlyActiveController = self.mapviewController; } 

MapViewController创build一个navigationBar和一个MKMapView。 现在我设置了硬编码的帧,因为我无法获得MapViewController的viewDidLoad()中窗口的边界/框架当我尝试获取有关bounds / frame的任何信息时,返回0。

 - (void)viewDidLoad { NSLog(@"MapviewController initialized"); [super viewDidLoad]; self.isMapViewPushedAside = NO; // Custom initizialation for navigationBar [self setupNavigationBar]; // Custom initialization for mapview [self setUpMapview]; [self trackUserLocation]; // Custom initialization for popupActionsButton [self setUpPopupButtons]; // Custom tests [self test]; [self focusLocationOnMap:self.locationManager.location.coordinate]; [self.locationManager stopUpdatingLocation]; } 

我已经实现了两个委托方法返回框架/边界(相同)的窗口。 问题是,我必须在一开始就得到这些值,而不是在所有事情都被初始化之后。 当一切都准备好之后,我从一个button调用委托方法时,它们按预期工作。

 CGRect frame = [self.mapDelegate frameData]; NSLog(@"width by frame: %f", frame.size.width); NSLog(@"height by frame: %f", frame.size.height); CGRect bounds = [self.mapDelegate boundsData]; NSLog(@"width by bounds: %f", bounds.size.width); NSLog(@"height by bounds: %f", bounds.size.height); 

如何获取框架/界限,即在调用我的自定义“设置”方法之前。?!

我有一个没有自己的看法的RootViewController。

没有视图你不能有一个UIViewController。 如果你有应用程序将崩溃。 当你初始化一个UIViewController它会自动为你创build一个UIView。

 - (void)viewDidLoad { [super viewDidLoad]; .. self.mapviewController = [[UCMapviewController alloc] initWithDelegate:self]; self.view = self.mapviewController.view; .. } 

从我在这里可以看到,你实际上是将RootviewController的视图设置为地图视图。 这应该通过覆盖你的控制器的-(void)loadView方法来完成,并且你需要设置视图:

 -(void)loadView { self.mapviewController = [[UCMapviewController alloc] initWithDelegate:self]; //if you're not using ARC this should be autoreleased; self.view = self.mapviewController.view; } 

当调用viewDidLoad方法时,在控制器的任何视图中都没有设置几何graphics。 他们只是初始化(隐式或明确的-(void)loadView )和viewDidLoad被调用。 几何最早在viewWillAppear:方法和连续的viewDidAppear:方法中设置,所以viewWillAppear:是最早的点,你可以有你的视图的实际框架/边界和viewWillAppear:方法,你应该只执行一些轻量级的操作(如设置几何,开始计时器,订阅观察员等)。
你说你不想改变你的方法,但你需要按照这些规则来devise。