有没有办法在视图和 xib 文件中的顶部布局指南之间添加约束? [英] Is there any way to add constraint between a view and the top layout guide in a xib file?

查看:14
本文介绍了有没有办法在视图和 xib 文件中的顶部布局指南之间添加约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS 7中,我们现在可以在视图和顶部布局指南之间添加约束,我认为这对于解决iOS7中的状态栏偏移问题非常有用(特别是当视图中没有导航栏时).

In iOS 7, we can now add a constraint between a view and the top layout guide, which I think is very useful to solve the status bar offset issue in iOS7(especially when there is no navigation bar in the view).

在故事板文件中,我可以轻松添加这种约束.只需按住控制键,然后将视图拖到容器中,它就会显示顶部空间到顶部布局指南"选项.

In a storyboard file, I can add this kind of constraints easily. Just hold the control key then drag the view to the container, it will show the "Top Space to Top Layout Guide" option.

但是当我在一个xib文件中做同样的操作时,这个选项就消失了.

But when I do the same operation in a xib file, this option disappears.

那么,有没有办法在xib文件中添加这种约束?还是我必须用代码添加它们?

So, is there any way to add this kind of constraints in xib files? Or do I have to add them with code?

推荐答案

你应该参考下面的例子,这肯定会帮助你解决你的问题.我从 http://developer.apple.com 得到这个.

You should refer the following example, this will definitely help you for your problem. I got this from http://developer.apple.com .

[button setTranslatesAutoresizingMaskIntoConstraints: NO];
id topGuide = myViewController.topLayoutGuide;
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings (button, topGuide);

[myViewController.view addConstraints:
    [NSLayoutConstraint constraintsWithVisualFormat: @"V:[topGuide]-20-[button]"
    options: 0
    metrics: nil
    views: viewsDictionary]
];

这篇关于有没有办法在视图和 xib 文件中的顶部布局指南之间添加约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆