【问题标题】:Navigation bar frame size calculation导航栏框尺寸计算
【发布时间】:2018-11-16 14:42:52
【问题描述】:

我正在使用以下几行来获取导航栏框架大小,它在 iOS 版本 11 之前工作正常,但在将我的 iOS 版本升级到 11 后,它返回我的 nil 值为 (0, 0)。谁能帮我解决这个问题。

CGRect frame = self.superview.frame;

            // Look for the navigation bar.
            UINavigationBar *navigationBar;
            UIView *superView = self;
            while (superView.superview)
            {
                if ([superView.superview isKindOfClass:[UINavigationBar class]])
                {
                    navigationBar = (UINavigationBar*)superView.superview;
                    break;
                }

                superView = superView.superview;
            }

            if (navigationBar)
            {
                CGSize navBarSize = navigationBar.frame.size;
                CGFloat superviewCenterX = frame.origin.x + (frame.size.width / 2);

                // Check whether the view is not moving away (see navigation between view controllers).
                if (superviewCenterX < navBarSize.width)
                {
                    // Center the display name
                    self.displayNameCenterXConstraint.constant = (navBarSize.width / 2) - superviewCenterX;
                }
            }

【问题讨论】:

    标签: ios objective-c uinavigationbar


    【解决方案1】:

    您永远不会初始化导航栏,所以我假设您正在尝试获取 null 的 navBarSize。

    如果您在具有导航栏的视图控制器中使用此代码,您可能会考虑以下方面:

    UINavigationBar *navigationBar = self.navigationController.navigationBar;
    CGSize navBarSize = navigationBar.frame.size;
    

    如果这不符合您的需求,我建议添加额外的示例代码/上下文,说明发生了什么问题或您正在尝试做什么。

    【讨论】:

    • 您是否无法使用您发布的代码获取框架?您在视图生命周期的哪个阶段使用此代码?您确定导航栏此时在屏幕上吗?如果 navBar 是其中一个超级视图的子视图,我不相信您的代码会起作用。
    猜你喜欢
    • 1970-01-01
    • 2019-02-14
    • 2017-04-15
    • 2017-08-20
    • 1970-01-01
    • 2010-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多