确定viewDidLoad中的框架/边界 [英] Determine frame/bounds in viewDidLoad

查看:62
本文介绍了确定viewDidLoad中的框架/边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好伙伴程序员,

首先,很抱歉的长篇文章。我的问题很简单,但我想确保你知道我在做什么,我真的不想改变我的方法的基本想法。

First, sorry for the long post. My question is rather simple, but I want to make sure you know what I'm doing and I really don't want to change the basic idea of my approach.

我有一个没有自己视图的RootViewController。他所做的就是实例化其他ViewControllers,管理它们的转换,并将它们的视图推入(添加)到主窗口中。要正确定位这些视图,我想为一个RootViewCOntrollers SubControllers获取边界/框架。这是我如何从appDelegate创建RootViewController(我开始一个空项目)

I have a RootViewController without an own view. All he does is instantiate other ViewControllers, manage their transitions and push (add) their views into the main window. To position these views correctly, I want to get bounds/frame for one of the RootViewCOntrollers SubControllers. This is how I create the RootViewController from the appDelegate (I started with a empty project)

- (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创建一个MapViewController

After his initialization, the rootviewController creates a 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创建一个navigationBar和一个MKMapView。现在我设置框架硬编码,因为我不能获得窗口的边界/框架在MapViewController的 viewDidLoad()当我尝试得到任何

The MapViewController creates a navigationBar and a MKMapView. Right now I set the frames hardcoded, because I'm not able to get the bounds/frame of the window in the viewDidLoad() of the MapViewController When I try to get any infos about bounds/frame, I get 0 returned.

- (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];
}



我已经实现两个委托方法返回框架/边界窗户。问题是,我必须在开始时获取这些值,而不是在所有内容都已初始化之后。当我从一个按钮调用委托方法一切就绪后,它们工作正常。

I've implemented two delegate methods that return frame/bounds (same) for the window. The problem is, I must get those values at the start, not after everything has been initialized. when I call the delegate methods from a button after everything is up, they work as expected.

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);

如何在开始时获取框架/边界,自定义设置方法...?!

推荐答案


自己的视图。

I have a RootViewController without an own view.

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

You can't have a UIViewController without a view. If you have the app will crash. When you initialize a UIViewController it automatically creates a UIView for you.

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

实际上将RootviewController的视图设置为地图视图。这应该通过覆盖你的控制器的 - (void)loadView 方法来完成,你需要设置视图:

From what can I see here, you're actually setting the RootviewController's view to be the map view. This should be done by overriding the -(void)loadView method of your controller and there you need to set the view:

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

viewDidLoad 被调用没有在您的控制器的任何视图中设置的几何。它们只被初始化(隐式或显式地由 - (void)loadView )和 viewDidLoad 几何是最早在 viewWillAppear:方法和连续的 viewDidAppear:方法中设置的,因此 viewWillAppear :是您可以拥有视图的实际框架/边界的最早点,在 viewWillAppear:方法中,您应该只执行一些轻量级操作设置几何,启动计时器,订阅观察者等)。

你说你不想改变方法,但是你需要根据这些规则进行设计。

When viewDidLoad method is called there is no geometry set in any of the views of your controller. They are only initialized (implicitly or explicitly by -(void)loadView) and viewDidLoad is called just right after that. Geometry is setup at earliest in viewWillAppear: method and the consecutive viewDidAppear: method, so viewWillAppear: is the earliest point you can have your actual frame/bounds of your views and in viewWillAppear: method you should only execute some lightweight operations (like setting geometry, starting timers, subscribe observers, etc..).
You said you don't want to change your approach, but you need to design according to these rules.

这篇关于确定viewDidLoad中的框架/边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆