简体   繁体   English

在我的viewcontroller中,我添加了一个带有xib的子视图,我在xib中设置了autolayout,但是当我将视图添加到视图控制器时它不起作用

[英]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.

相关问题 为什么我不能在XIB中选择视图控制器? - Why can't I segue to a view controller in my XIB? 如何快速将xib视图加载到viewController中? - How can I load fast a xib view into my viewController? Swift-以编程方式将自定义Xib视图添加为子视图 - Swift - Add Custom Xib View As Subview Programmatically 将自定义子视图(在xib中创建)添加到视图控制器的视图中 - 我做错了什么 - Adding a custom subview (created in a xib) to a view controller's view - What am I doing wrong iOS在xib中添加视图,但我仍然需要使用addView - iOS Add a view in xib but i still need to use addView 情节提要添加一个自定义视图,该视图的xib文件没有子视图 - Storyboard add a custom view which has xib file has no subviews 无法添加新的xib来查看控制器 - Cant add new xib's to view controller 将视图添加为子视图时,为什么看不到整个选项卡栏? - Why can't i see my whole tab bar when i add it in a view as subview? 如何将另一个视图控制器的子视图链接到使用自动布局的当前Xib - How can I link another view controller's sub view into current xib which is using auto layout 如何添加向xib添加导航控制器 - How do i add add a navigation controller to a xib
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM