ios 添加子视图控制器 swift
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25404484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add child view controller swift
提问by jmcastel
I m trying to add child view controller to a containerViewController
我正在尝试将子视图控制器添加到 containerViewController
Child are :
孩子是:
AViewController
BViewController
CViewController
I have no error but when i launch the app, i can swipe the screen, there is 3 section swiped but the A,B,C view controllers don't appeared..
我没有错误,但是当我启动应用程序时,我可以滑动屏幕,滑动了 3 个部分,但是 A、B、C 视图控制器没有出现..
This is my code, any idea ?
这是我的代码,知道吗?
import UIKit
class ContainerViewController: UIViewController {
@IBOutlet var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// creat VC
var Avc : AViewController = AViewController ()
var Bvc : BViewController = BViewController ()
var Cvc : CViewController = CViewController ()
// add it to the view hierarchie
self.addChildViewController(Cvc)
self.scrollView.addSubview(Cvc.view)
Cvc.didMoveToParentViewController(self)
self.addChildViewController(Bvc)
self.scrollView.addSubview(Bvc.view)
Bvc.didMoveToParentViewController(self)
self.addChildViewController(Avc)
self.scrollView.addSubview(Avc.view)
Avc.didMoveToParentViewController(self)
// set the frame
var adminFrame : CGRect = Avc.view.frame
adminFrame.origin.x = adminFrame.width
Bvc.view.frame = adminFrame
var BFrame : CGRect = Bvc.view.frame
BFrame.origin.x = 2*BFrame.width
Cvc.view.frame = BFrame
// set the frame of the scrollview
var scrollWidth: CGFloat = 3*self.view.frame.width
var scrollHeight: CGFloat = self.view.frame.size.height
self.scrollView.contentSize = CGSizeMake(scrollWidth, scrollHeight)
}
Edit:
编辑:
Looking at the view hierarchy, it reports the following:
查看视图层次结构,它报告以下内容:
<UIWindow: 0x7ff3fad19f70; frame = (0 0; 320 568); gestureRecognizers = <NSArray: 0x7ff3fac3efe0>; layer = <UIWindowLayer: 0x7ff3fad19740>>
| <UIView: 0x7ff3fb108b90; frame = (0 0; 320 568); autoresize = W+H; layer = <CALayer: 0x7ff3fb108e60>>
| | <UIScrollView: 0x7ff3fac3acf0; frame = (0 0; 320 568); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x7ff3fb107320>; layer = <CALayer: 0x7ff3fac18e00>; contentOffset: {0, 0}; contentSize: {960, 568}>
| | | <UIView: 0x7ff3fac41ed0; frame = (640 0; 320 568); autoresize = W+H; layer = <CALayer: 0x7ff3fac28a00>>
| | | <UIView: 0x7ff3fac42320; frame = (320 0; 320 568); autoresize = W+H; layer = <CALayer: 0x7ff3fac38e10>>
| | | <UIView: 0x7ff3fac42730; frame = (0 0; 320 568); autoresize = W+H; layer = <CALayer: 0x7ff3fac42810>>
| | | <UIImageView: 0x7ff3faf020f0; frame = (0 564.5; 320 3.5); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x7ff3fae0df80>>
| | | <UIImageView: 0x7ff3fac1c660; frame = (316.5 0; 3.5 568); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x7ff3fac39420>>
| | <_UILayoutGuide: 0x7ff3fb108ec0; frame = (0 0; 0 20); hidden = YES; layer = <CALayer: 0x7ff3fb1091e0>>
| | <_UILayoutGuide: 0x7ff3fb109c20; frame = (0 568; 0 0); hidden = YES; layer = <CALayer: 0x7ff3fb109d00>>
回答by Phani Sai
Use following Extension for adding ChildviewController create file Extensions.swift copy bellow code
使用以下扩展添加 ChildviewController 创建文件 Extensions.swift 复制波纹管代码
import UIKit
extension UIViewController {
func configureChildViewController(childController: UIViewController, onView: UIView?) {
var holderView = self.view
if let onView = onView {
holderView = onView
}
addChildViewController(childController)
holderView?.addSubview(childController.view)
constrainViewEqual(holderView: holderView!, view: childController.view)
childController.didMove(toParentViewController: self)
}
func constrainViewEqual(holderView: UIView, view: UIView) {
view.translatesAutoresizingMaskIntoConstraints = false
//pin 100 points from the top of the super
let pinTop = NSLayoutConstraint(item: view, attribute: .top, relatedBy: .equal,
toItem: holderView, attribute: .top, multiplier: 1.0, constant: 0)
let pinBottom = NSLayoutConstraint(item: view, attribute: .bottom, relatedBy: .equal,
toItem: holderView, attribute: .bottom, multiplier: 1.0, constant: 0)
let pinLeft = NSLayoutConstraint(item: view, attribute: .left, relatedBy: .equal,
toItem: holderView, attribute: .left, multiplier: 1.0, constant: 0)
let pinRight = NSLayoutConstraint(item: view, attribute: .right, relatedBy: .equal,
toItem: holderView, attribute: .right, multiplier: 1.0, constant: 0)
holderView.addConstraints([pinTop, pinBottom, pinLeft, pinRight])
}
}
User view controller
用户视图控制器
import UIKit
class MyViewControler:UIViewControler {
@IBOutlet weak var myView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let demoViewInstance = storyboard!.instantiateViewController(withIdentifier: "youChiledViewController_Id") as! childViewController
configureChildViewController(childController: demoViewInstance, onView: myView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
回答by Rob
When I use your code, run the app, and pause the app, and look at the view hierarchy, I see the following:
当我使用您的代码、运行应用程序、暂停应用程序并查看视图层次结构时,我看到以下内容:
(lldb) po [[UIWindow keyWindow] recursiveDescription]
<UIWindow: 0x156bdc30; frame = (0 0; 320 568); gestureRecognizers = <NSArray: 0x156be750>; layer = <UIWindowLayer: 0x156aa3c0>>
| <UIView: 0x156c5440; frame = (0 0; 320 568); autoresize = W+H; layer = <CALayer: 0x156c55c0>>
| | <UIScrollView: 0x156c2740; frame = (0 0; 0 568); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x156c4d00>; layer = <CALayer: 0x156c2a80>; contentOffset: {0, 0}; contentSize: {960, 568}>
| | | <UIView: 0x156c6df0; frame = (640 0; 0 536); autoresize = W+H; layer = <CALayer: 0x156c6d80>>
| | | <UIView: 0x156c7100; frame = (320 0; 0 536); autoresize = W+H; layer = <CALayer: 0x156c70a0>>
| | | <UIView: 0x156c73f0; frame = (0 0; 0 536); autoresize = W+H; layer = <CALayer: 0x156c7390>>
| | | <UIImageView: 0x156c8bd0; frame = (0 564.5; 600 3.5); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x156c8c50>>
| | | <UIImageView: 0x156c9020; frame = (-3.5 32; 3.5 568); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x156c90a0>>
| | <_UILayoutGuide: 0x156c5620; frame = (0 0; 0 20); hidden = YES; layer = <CALayer: 0x156c5800>>
| | <_UILayoutGuide: 0x156c5c90; frame = (0 568; 0 0); hidden = YES; layer = <CALayer: 0x156c5d10>>
If you're not seeing your subviews there, then likely culprits include:
如果您没有在那里看到您的子视图,那么可能的罪魁祸首包括:
You may not have specified the view controller base class in your scene, and thus this code isn't being run. You can confirm this with a
println
log statement or breakpoint in side thisviewDidLoad
and make sure you're hitting this routine.You may not have hooked up the
@IBOutlet
for thescrollView
, and thusscrollView
isnil
. Confirm this by putting breakpoint inviewDidLoad
and examining thescrollView
property.
您可能没有在场景中指定视图控制器基类,因此不会运行此代码。您可以使用
println
日志语句或在此旁边的断点来确认这一点viewDidLoad
,并确保您正在执行此例程。您可能没有
@IBOutlet
为 连接scrollView
,因此scrollView
是nil
。通过放置断点viewDidLoad
并检查scrollView
属性来确认这一点。
In your revised question, we can now see that the three subviews are present and appear to be the correct size. That's great.
在您修改后的问题中,我们现在可以看到三个子视图都存在并且看起来是正确的大小。那太棒了。
Now the question is why you don't see anything. If you have these defined as scenes in your storyboard, you should:
现在的问题是为什么你什么都看不到。如果您在故事板中将这些定义为场景,您应该:
Make sure the "base class" and the "storyboard identifier" is defined for each of these three child scenes; and
When your main view controller instantiates the three child view controllers, you would instantiate them from the storyboard using the storyboard identifier (in my example, I used storyboard identifiers of
A
,B
, andC
, respectively):let Avc = storyboard.instantiateViewControllerWithIdentifier("A") as AViewController let Bvc = storyboard.instantiateViewControllerWithIdentifier("B") as BViewController let Cvc = storyboard.instantiateViewControllerWithIdentifier("C") as CViewController
确保为这三个子场景中的每一个都定义了“基类”和“故事板标识符”;和
当你的主视图控制器实例的三个子视图控制器,你会用故事板标识,从故事板实例化它们(在我的例子,我用故事板标识符
A
,B
和C
,分别对应)。let Avc = storyboard.instantiateViewControllerWithIdentifier("A") as AViewController let Bvc = storyboard.instantiateViewControllerWithIdentifier("B") as BViewController let Cvc = storyboard.instantiateViewControllerWithIdentifier("C") as CViewController
If you do the above and repeat the recursiveDescription
you should see your scene's subviews (e.g. the labels you added) appear in the output.
如果您执行上述操作并重复,recursiveDescription
您应该会看到您场景的子视图(例如您添加的标签)出现在输出中。