[英]in my viewcontroller, I add a subview which has a xib, I set the autolayout in the xib, but when i add the view to the view controller it doesn't work
first I have my custom view named FindFriendHeadPublish.h,which contains a xib file as follows,I have set the autolayout,and the view is OC 首先我有一个名为FindFriendHeadPublish.h的自定义视图,它包含一个xib文件,如下所示,我设置了autolayout,视图是OC
and my view controller which named PreviewViewController.swift also has a xib and I add the FindFriendHeadPublish view to the view controller, but the autolayout in the FindFriendHeadPublish doesn't work 名为PreviewViewController.swift的视图控制器也有一个xib,我将FindFriendHeadPublish视图添加到视图控制器,但FindFriendHeadPublish中的autolayout不起作用
override func viewDidLoad() {
super.viewDidLoad()
self.title = "我的卡片预览"
var addpreviewView = NSBundle.mainBundle().loadNibNamed("FindFriendHeadPublish", owner: nil, options: nil)
var previewView = addpreviewView.last as! FindFriendHeadPublish
previewView.setTranslatesAutoresizingMaskIntoConstraints(false);
GetUserInfoInterface.getUserInfoWithFUid(UserInfoSaveUtil.getUserInfo().userid, withBlock: { (userInfo:GetUserInfoVo! , error:NSError! ) -> Void in
})
self.scrollView.contentSize = CGSizeMake(320, 640)
self.scrollView.addSubview(previewView)
}
I have check some other problems , but I haven't find a solution, any one can help, thank you 我已经检查了一些其他问题,但我找不到解决方案,任何人都可以提供帮助,谢谢
further more, the custom view works well in another viewcontroller which write in OC 此外,自定义视图在另一个在OC中写入的viewcontroller中运行良好
and the code above works as follows 以上代码的工作原理如下
First of all add Hieght Constraint to previewView in the nib then add the following code after previewView.setTranslatesAutoresizingMaskIntoConstraints(false);
首先将Hieght Constraint添加到nib中的previewView,然后在
previewView.setTranslatesAutoresizingMaskIntoConstraints(false);
之后添加以下代码previewView.setTranslatesAutoresizingMaskIntoConstraints(false);
var constraints: NSMutableArray = NSMutableArray()
constraints.addObject(NSLayoutConstraint(item: previewView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.scrollView, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0.0))
constraints.addObject(NSLayoutConstraint(item: previewView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self.scrollView, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0.0))
constraints.addObject(NSLayoutConstraint(item: previewView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self.scrollView, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0.0))
self.scrollView.addConstraints(constraints as [AnyObject])
Hope that helps. 希望有所帮助。
You did not set the constraints for previewView and its superview, which you cant do in IB if the views are not in one file. 您没有为previewView及其superview设置约束,如果视图不在一个文件中,则无法在IB中执行。
previewView.setTranslatesAutoresizingMaskIntoConstraints(false);
let viewBindingsDict = ["previewView":previewView, "view":self.view]
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[previewView(==view)]", options: nil, metrics: nil, views: viewBindingsDict))
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[previewView]", options: nil, metrics: nil, views: viewBindingsDict))
this example sets the previewView top left with the width of the controllers view. 此示例将previewView左上角设置为控制器视图的宽度。 a nice tutorial: http://www.thinkandbuild.it/learn-to-love-auto-layout-programmatically/
一个很好的教程: http : //www.thinkandbuild.it/learn-to-love-auto-layout-programmatically/
it is recommended to use only one view in a scrollview and use it as container for the others. 建议在scrollview中仅使用一个视图,并将其用作其他视图的容器。 if you load a nib and set the owner to self
如果你加载一个笔尖并将所有者设置为自己
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.