问题描述
我试图用控制器 UserView
添加到使用X code视图7 /雨燕2.0。
我无法弄清楚如何做到这一点使用Interface Builder,而不是我添加了一个的NSView
作为占位符视图,然后在code添加视图
是否有可能在界面生成器呢?
在图像,橙色是占位和绿色是用户视图:
当加在code中的观点,我想我要补充的限制。我试图使用 constraintsWithVisualFormat
象下面这样,但是当我添加此,认为不能调整大小。我想这可能涉及到的重点。
我如何获得绿色子视图来填充占位容器,并调整大小?
覆盖FUNC windowDidLoad(){
super.windowDidLoad()
打印(MainWindowController.windowDidLoad); 让视图= UserView01();
self.m_viewPlaceHolder.addSubview(view.view); 让HOR = NSLayoutConstraint.constraintsWithVisualFormat(H:| -10- [查看] -10 |选项:.AlignAllLeft,指标:无,意见:查看:view.view]);
让版本= NSLayoutConstraint.constraintsWithVisualFormat(V:| -10- [查看] -10 |选项:.AlignAllLeft,指标:无,意见:查看:view.view]);
NSLayoutConstraint.activateConstraints(HOR);
NSLayoutConstraint.activateConstraints(VER);
}
首先,没有必要添加任何占位符,您以编程方式创建视图。如果你想看到它在故事板,我建议你做视图类 @IBDesignable
:
@IBDesignable类UserView01:UIView的{ //类的声明}
要被改变,因为你code应被声明为 @IBInspectable
类似于类声明的属性。
要确保编程设定的限制被执行有约束code之前添加以下行:
view.translatesAutoresizingMaskIntoConstraints = FALSE
它告诉X code到使用您设置的限制,但不尝试自动添加任何,通常X code那样被。除此之外, - 你的code应该可以正常工作
也有另一种方式来增加您可能使用,以及相对的约束:
让leadingConstraint = NSLayoutConstraint(项目:视图,属性:.Leading,relatedBy:.Equal,toItem:占位符,属性:.Leading,事半功倍:1,恒:20)
让trailingConstraint = NSLayoutConstraint(项目:视图,属性:.Trailing,relatedBy:.Equal,toItem:占位符,属性:.Trailing,事半功倍:1,恒:20)
让topConstraint = NSLayoutConstraint(项目:视图,属性:.TOP,relatedBy:.Equal,toItem:占位符,属性:.TOP,事半功倍:1,恒:20)
让bottomConstraint = NSLayoutConstraint(项目:视图,属性:.Bottom,relatedBy:.Equal,toItem:占位符,属性:.Bottom,事半功倍:1,恒:20) view.addConstraints([leadingConstraint,trailingConstraint,topConstraint,bottomConstraint])
只要改变视图
和占位符
来使用的视图及其在项目上海华的名字。
I'm trying to add a UserView
with controller to a view with Xcode 7 / Swift 2.0.
I was not able to figure out how to do this using Interface Builder, instead I added a NSView
as a placeholder view and then add the view in code.
Is it possible to do it with the Interface Builder?
In the image, the orange is the place holder and the green is the user view:
When adding the view in code, I assume I have to add the constraints. I tried to use the constraintsWithVisualFormat
like below, but when I add this, the view cannot be resized. I think it might be related to the priorities.
How do I get the green subview to fill the place holder container and be resizable?
override func windowDidLoad() {
super.windowDidLoad()
print("MainWindowController.windowDidLoad");
let view = UserView01();
self.m_viewPlaceHolder.addSubview(view.view);
let hor = NSLayoutConstraint.constraintsWithVisualFormat("H:|-10-[view]-10-|", options: .AlignAllLeft, metrics: nil, views: ["view" : view.view]);
let ver = NSLayoutConstraint.constraintsWithVisualFormat("V:|-10-[view]-10-|", options: .AlignAllLeft, metrics: nil, views: ["view" : view.view]);
NSLayoutConstraint.activateConstraints(hor);
NSLayoutConstraint.activateConstraints(ver);
}
First of all, there is no need to add any placeholders for the view you create programmatically. If you want to see it in the storyboard, I'd suggest you make the view class @IBDesignable
:
@IBDesignable class UserView01: UIView {
// class declaration
}
The properties you want to be changing as you code should be declared as @IBInspectable
similarly to class declaration.
To ensure the programmatically set constraints are executed you have to add the following line before the constraints code:
view.translatesAutoresizingMaskIntoConstraints = false
It tells Xcode to use the constraints you set without trying to add any automatically, which normally Xcode does. Other than that - your code should work fine.
There is also another way to add relative constraints which you might use as well:
let leadingConstraint = NSLayoutConstraint(item: view, attribute: .Leading, relatedBy: .Equal, toItem: placeholder, attribute: .Leading, multiplier: 1, constant: 20)
let trailingConstraint = NSLayoutConstraint(item: view, attribute: .Trailing, relatedBy: .Equal, toItem: placeholder, attribute: .Trailing, multiplier: 1, constant: 20)
let topConstraint = NSLayoutConstraint(item: view, attribute: .Top, relatedBy: .Equal, toItem: placeholder, attribute: .Top, multiplier: 1, constant: 20)
let bottomConstraint = NSLayoutConstraint(item: view, attribute: .Bottom, relatedBy: .Equal, toItem: placeholder, attribute: .Bottom, multiplier: 1, constant: 20)
view.addConstraints([leadingConstraint, trailingConstraint, topConstraint, bottomConstraint])
Just change the view
and placeholder
to the names you use for the view and its superview in your project.
这篇关于如何以编程方式添加自定义的UIView与斯威夫特,看看它在Interface Builder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!