首页 > 解决方案 > Swift:为 iPad 屏幕调整 NavigationBar 的大小

问题描述

我有一个导航栏,在 iphone 设备上看起来很完美,但在 iPad 上打开时它看起来很小。我正在使用情节提要进行设计,如何调整导航栏的大小以及 ipad 的字体。

在此处输入图像描述

标签: iosswiftuinavigationcontrolleruinavigationbar

解决方案


导航栏的高度与状态栏固定为 65。但如果你想改变导航栏的高度,你可以做以下替代方式。

let height: CGFloat = 50 //whatever height you want to add to the existing height
let bounds = self.navigationController!.navigationBar.bounds
self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height + height)

并用于设置字体。

if (UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad)
        {
            UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName :NavigationColor,
                                                                NSFontAttributeName: FontLight_40
            ]
            UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName:FontLight_40], for: UIControlState.normal)
        }
        else
        {
            UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName :NavigationColor,
                                                                NSFontAttributeName: FontLight_20
            ]
            UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName:FontLight_20], for: UIControlState.normal)
        }

推荐阅读