2016-10-04 80 views
0

我使用iOS 9约束来以编程方式设置我的应用程序的布局。我明白创建一个导航栏编程我做了以下内容:如何根据设备更改导航栏的大小?

let navigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: My_Height)) 

的事情是,我想的导航栏的高度改变的设备改变,使其使用约束比例。我会怎么做呢?

回答

0

你必须设置高度不变,以约束编程制作的出口,并且计算比例

+0

我该怎么做呢? –

+0

@property(弱,非原子)IBOutlet NSLayoutConstraint * heightConstraint; – Ashish

+0

创建IBOutlet并根据设备高度计算比例值 – Ashish

0

您可以使用此屏幕尺寸:

var screenWidth: CGFloat { 
    if UIInterfaceOrientationIsPortrait(screenOrientation) { 
     return UIScreen.mainScreen().bounds.size.width 
    } else { 
     return UIScreen.mainScreen().bounds.size.height 
    } 
} 
var screenHeight: CGFloat { 
    if UIInterfaceOrientationIsPortrait(screenOrientation) { 
     return UIScreen.mainScreen().bounds.size.height 
    } else { 
     return UIScreen.mainScreen().bounds.size.width 
    } 
} 
var screenOrientation: UIInterfaceOrientation { 
    return UIApplication.sharedApplication().statusBarOrientation 
} 

然后,您可以自定义导航的尺寸取决于屏幕的高度或宽度。