【问题标题】:Get frame of Navigation Bar with UINavigationBar appearance获取具有 UINavigationBar 外观的导航栏框架
【发布时间】:2014-04-26 21:19:35
【问题描述】:

抱歉,问题很简单,但代码如下:

[[UINavigationBar appearance] setBackgroundImage:[UIImage
    imageNamed:@"backgroundNavigationBar.png"] forBarMetrics:UIBarMetricsDefault];
CGRect frame = [[UINavigationBar appearance] frame];
NSLog(@"%@", NSStringFromCGRect(frame));
UIView *view = [[UIView alloc] initWithFrame:frame];
[[UINavigationBar appearance] addSubview: view];

给我

{{0, 0}, {0, 0}}

如何正确获取当前导航栏的实际尺寸,即 320x64 的框架?

【问题讨论】:

    标签: objective-c ios7 uinavigationbar uiappearance


    【解决方案1】:

    试试:

    CGRect frame = self.navigationController.navigationBar.frame;
    

    【讨论】:

      【解决方案2】:

      恐怕您的代码无法按照您希望的方式运行:/

      外观代理当然不是用来添加子视图的!

      您只能通过 UIAppearance 编辑在头文件中标记为“UI_APPEARANCE_SELECTOR”的属性!

      编译器允许您编写代码,因为+ (instancetype)appearance 实际上返回了该类的代理对象。

      您要添加子视图的实际视图与该代理没有任何关系。 UIAppearance 查看代理对象,并在创建新视图时仅复制 UI_APPEARANCE_SELECTOR 标记的属性。

      请阅读此https://developer.apple.com/library/ios/documentation/uikit/reference/UIAppearance_Protocol/Reference/Reference.html

      还有这个http://nshipster.com/uiappearance/ ,它将为您提供如何使用 UIAppearance 的一个很好的概述。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-11-10
        • 1970-01-01
        • 2016-03-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-26
        相关资源
        最近更新 更多