簡體   English   中英

在viewDidLoad上動態確定ViewController的視圖大小

[英]Determine viewcontroller's view size dynamically on viewDidLoad

我已經搜索了很多有關如何在viewDidLoad方法中動態確定實際視圖大小的方法。

這是我的方法,似乎在橫向和縱向模式下都適用於iOS6和iOS7。

有更好的解決方案嗎?

這是我的代碼:

- (CGSize)actualSize {
    CGFloat widht;
    CGFloat height;

    // Determin height and widht
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    if (orientation == UIDeviceOrientationLandscapeRight ||  orientation == UIDeviceOrientationLandscapeLeft) {
        widht = kHeightSelfView;
        height = kWidthSelfView;
    } else {
        widht = kWidthSelfView;
        height = kHeightSelfView;
    }

    // Determin navigationbar height
    CGFloat navigationBarHeight = self.navigationController.navigationBarHidden ? .0f : (MIN(self.navigationController.navigationBar.frame.size.height, self.navigationController.navigationBar.frame.size.width));

    // Determin navigationbar height
    CGFloat statusbarHeight = [DeviceCompatibility isIOS7] ? (MIN(SharedApplication.statusBarFrame.size.width, SharedApplication.statusBarFrame.size.height)) : .0f;

    return CGSizeMake(widht, height - navigationBarHeight - statusbarHeight);
}

通常,您只應在viewWillAppear中執行基於視圖的幾何。 視圖的框架尚未在viewDidLoad中設置,但是在調用viewWillAppear之前將正確設置它。

除了選擇高度和寬度,您還可以像這樣選擇視圖框架:

// Adjusts the frame of the child view.
    CGRect frame = self.view.frame;
    CGRect linkedFrame = scene.view.frame;
    linkedFrame.origin.x -= frame.origin.x;
    linkedFrame.origin.y -= frame.origin.y;

然后您可以使其靈活,以便正確調整大小:

// The scene's main view must be made flexible so it will resize properly
        // in the container.
        scene.view.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
                                       UIViewAutoresizingFlexibleHeight);

讓我知道這是否不是您所需要的:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM