2015-06-06 11 views
4

首先我有我的自定義視圖名爲FindFriendHeadPublish.h,其中包含了廈門國際銀行文件如下,我已經設置了自動版式,視圖是OC enter image description here在我的viewcontroller中,我添加了一個子視圖,其中有一個xib,我在xib中設置了自動佈局,但是當我將視圖添加到視圖控制器時,它不起作用

並命名PreviewViewController.swift也有廈門國際銀行 我的視圖控制器和我的FindFriendHeadPublish視圖添加到視圖控制器,但FindFriendHeadPublish中的自動佈局不起作用

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) 

} 

我查了一些其他的問題,但我還沒有找到一個解決方案,任何一個能幫助,謝謝

進一步,自定義視圖效果很好,其中的OC

和代碼編寫另一個視圖 - 控制上面的工作如下 enter image description here

+0

請分享您的代碼。 – Rajesh

回答

0

您沒有設置previewView及其超級視圖的約束條件,如果視圖不在一個文件中,您無法在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)) 

此示例將previewView左上角設置爲控制器視圖的寬度。 一個不錯的教程:http://www.thinkandbuild.it/learn-to-love-auto-layout-programmatically/

建議在scrollview中只使用一個視圖並將其用作其他視圖的容器。 如果您加載筆尖和業主設置爲自

+0

我嘗試了你提供的代碼,但它與「NSScanner:nil string argument」一起崩潰 – zyunchen

1

首先添加Hieght約束previewView在筆尖 再經過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]) 

希望幫助添加以下代碼。

+0

只是一個更正,在'self.scrollView.addSubview(previewView)後面寫上面的代碼片段' – uchiha

相關問題