- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view,typically from a nib.

    ChildViewController *childviewcontroller = [[ChildViewController alloc] initWithNibName:@"ChildViewController" bundle:nil];


    [self addChildViewController:childviewcontroller];
    [self.view addSubview:childviewcontroller.view];
    [childviewcontroller willMovetoParentViewController:self];
    UIView *cview = [[UIView alloc] init];
    cview = childviewcontroller.view;
    [self.view removeConstraints:self.view.constraints];

    NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(cview);
    [self.view addConstraints:[NSLayoutConstraint 
                                constraintsWithVisualFormat:@"H:|-[cview]-|" 
                                                    options:0 metrics:nil                                        
                                                    views:viewsDictionary]];

}

我想在父视图上添加childviewcontroller视图.添加后我设置了约束,但它对我不起作用.

我也收到这样的警告

2013-07-25 10:47:30.564 neenah[1105:c07] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: (1) look at each constraint and try to figure out which you don't expect; 
    (2) find the code that added the unwanted constraint or constraints and fix it. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't 
    understand,refer to the documentation for the UIView 
    property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x9256c90 H:|-(Nsspace(20))-[UIView:0x9256a00]   (Names: '|':UIView:0x92557a0 )>","<NSAutoresizingMaskLayoutConstraint:0x755d690 h=--& v=--& H:[UIView:0x9256a00(320)]>","<NSAutoresizingMaskLayoutConstraint:0x755d5a0 h=--& v=--& UIView:0x9256a00.midX == + 160>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x9256c90 H:|-(Nsspace(20))-[UIView:0x9256a00]   (Names: '|':UIView:0x92557a0 )>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2013-07-25 10:47:30.567 neenah[1105:c07] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand,refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x9256bb0 H:[UIView:0x9256a00]-(Nsspace(20))-|   (Names: '|':UIView:0x92557a0 )>","<NSAutoresizingMaskLayoutConstraint:0x7561690 h=--- v=--- H:[UIWindow:0x92527c0(320)]>","<NSAutoresizingMaskLayoutConstraint:0x755fe50 h=-&- v=-&- UIView:0x92557a0.width == UIWindow:0x92527c0.width>","<NSAutoresizingMaskLayoutConstraint:0x755d5a0 h=--& v=--& UIView:0x9256a00.midX == + 160>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x9256bb0 H:[UIView:0x9256a00]-(Nsspace(20))-|   (Names: '|':UIView:0x92557a0 )>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

解决方法

一些观察:

>你应该关闭translatesAutoresizingMaskIntoConstraints:

childviewcontroller.view.translatesAutoresizingMaskIntoConstraints = NO;

>您还应该定义垂直约束:

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[cview]-|" 
                                                                  options:0
                                                                  metrics:nil                                        
                                                                    views:viewsDictionary]];

>与您的问题无关,您不需要为cview创建[[UIView alloc] init].你马上放弃它.
>我不确定你为什么要删除self.view的约束. (我假设你这样做是因为你在测试时撕掉了你的头发.)你不必这样做.但如果你在这里有其他事情让你认为你需要这样做,那么让我们知道那是什么.
>添加子控制器时,调用didMovetoParentViewController,而不是willMovetoParentViewController. addChildViewController为您调用willMovetoParentViewController.你只需要didMove …再现.

从而:

- (void)viewDidLoad {
    [super viewDidLoad];

    // instantiate the view controller

    ChildViewController *childViewController = [[ChildViewController alloc] initWithNibName:@"ChildViewController" bundle:nil];

    // or you can instantiate using storyboard
    //
    // ChildViewController *child = [self.storyboard instantiateViewControllerWithIdentifier:@"ChildIdentifier"];

    // Now do the view controller containment calls to update the view controller hierarchy and add the view as appropriate

    [self addChildViewController:childViewController];
    childViewController.view.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:childViewController.view];
    [childViewController didMovetoParentViewController:self];
    UIView *childView = childViewController.view;

    NSDictionary *views = NSDictionaryOfVariableBindings(childView);
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[childView]-|" options:0 metrics:nil views:views]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[childView]-|" options:0 metrics:nil views:views]];
}

ios – 如何在父视图上添加子视图控制器的视图的更多相关文章

  1. ios – 将UIViewController视图属性设置为不带有storyboard / nib的自定义UIView类

    或者上面的代码片段是推荐的吗?

  2. ios – 如何在父视图上添加子视图控制器的视图

    再现.从而:

  3. ios – 实例化ViewController w / xib时的帧大小不正确

    我有一个视图控制器,看起来像:当我通过让spotViewController=SpotViewController实例化视图控制器并将其推送到导航控制器时,结果框架在viewDidLoad和viewWillAppear中都是不正确的.它给了我这是界面构建器中的大小.为什么会发生这种情况以及使用xib实例化视图控制器以确保帧正确的正确方法是什么?解决方法这将解决您的问题,UIViewController从xib加载,在viewDidLoad期间保持xib大小:

  4. ios – CAGradientLayer不工作

    参见英文答案>DrawinggradientonUIViewnotworkingwithiOS91个我创建了一个新项目.我链接了QuartzCore.framework并导入了在ViewController.m中.这是代码.我尝试将视图的背景颜色设置为clearColor,在viewDidAppear中调用它,但它们都没有工作.我真的不知道自己错过

  5. ios – UIView-Encapsulated-Layout-Height和Container View

    我怎么能跳过它?

  6. ios – 使用自动布局的基于百分比的保证金

    我只想让左边距为父视图宽度的0.75%.谢谢.解决方法你想要的是sview的左边是在视图左边的某个点,你写的是你希望sview的左边是视图宽度的某个点,这不是一个正确的布局属性配对作为你的错误说.这是你需要做的:希望能帮助到你!

  7. Swift 简单控件示例:活动指示器UIActivityIndicatorView

    转载请声明出处:http://www.jb51.cc/article/p-fycefhzj-ga.html---------------------------------------------------------------------------------------------------------------------------------------------------

  8. [anyObject] 类型推断 Type Casting

    在swift中,如果遍历一个集合,可能就需要用到类型推断方式二:适合多种类型的混合,在for循环里,如果是多种类型,就进行多种类型的判断添加if-else

  9. Swift 文本编辑时将文本框上移

  10. UIScrollView加载子视图偏移64的问题

    最近正在学swift,基本语法刚刚看完,试着实践一下。没想到用scrollView的时候遇到问题了。o(╯□╰)o在一个VC里如果第一个控件是UIScrollView,注意是第一个控件,就是首先addsubview在VC.view上。接着加到scrollView上的View就会在Y点上发生64的偏移。这个在iOS7以后才会出现。如果这个scrollView不是第一个加到self.view上的。

随机推荐

  1. iOS实现拖拽View跟随手指浮动效果

    这篇文章主要为大家详细介绍了iOS实现拖拽View跟随手指浮动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  2. iOS – genstrings:无法连接到输出目录en.lproj

    使用我桌面上的项目文件夹,我启动终端输入:cd然后将我的项目文件夹拖到终端,它给了我路径.然后我将这行代码粘贴到终端中找.-name*.m|xargsgenstrings-oen.lproj我在终端中收到此错误消息:genstrings:无法连接到输出目录en.lproj它多次打印这行,然后说我的项目是一个目录的路径?没有.strings文件.对我做错了什么的想法?

  3. iOS 7 UIButtonBarItem图像没有色调

    如何确保按钮图标采用全局色调?解决方法只是想将其转换为根注释,以便为“回答”复选标记提供更好的上下文,并提供更好的格式.我能想出这个!

  4. ios – 在自定义相机层的AVFoundation中自动对焦和自动曝光

    为AVFoundation定制图层相机创建精确的自动对焦和曝光的最佳方法是什么?

  5. ios – Xcode找不到Alamofire,错误:没有这样的模块’Alamofire’

    我正在尝试按照github(https://github.com/Alamofire/Alamofire#cocoapods)指令将Alamofire包含在我的Swift项目中.我创建了一个新项目,导航到项目目录并运行此命令sudogeminstallcocoapods.然后我面临以下错误:搜索后我设法通过运行此命令安装cocoapodssudogeminstall-n/usr/local/bin

  6. ios – 在没有iPhone6s或更新的情况下测试ARKit

    我在决定下载Xcode9之前.我想玩新的框架–ARKit.我知道要用ARKit运行app我需要一个带有A9芯片或更新版本的设备.不幸的是我有一个较旧的.我的问题是已经下载了新Xcode的人.在我的情况下有可能运行ARKit应用程序吗?那个或其他任何模拟器?任何想法或我将不得不购买新设备?解决方法任何iOS11设备都可以使用ARKit,但是具有高质量AR体验的全球跟踪功能需要使用A9或更高版本处理器的设备.使用iOS11测试版更新您的设备是必要的.

  7. 将iOS应用移植到Android

    我们制作了一个具有2000个目标c类的退出大型iOS应用程序.我想知道有一个最佳实践指南将其移植到Android?此外,由于我们的应用程序大量使用UINavigation和UIView控制器,我想知道在Android上有类似的模型和实现.谢谢到目前为止,guenter解决方法老实说,我认为你正在计划的只是制作难以维护的糟糕代码.我意识到这听起来像很多工作,但从长远来看它会更容易,我只是将应用程序的概念“移植”到android并从头开始编写.

  8. ios – 在Swift中覆盖Objective C类方法

    我是Swift的初学者,我正在尝试在Swift项目中使用JSONModel.我想从JSONModel覆盖方法keyMapper,但我没有找到如何覆盖模型类中的Objective-C类方法.该方法的签名是:我怎样才能做到这一点?解决方法您可以像覆盖实例方法一样执行此操作,但使用class关键字除外:

  9. ios – 在WKWebView中获取链接URL

    我想在WKWebView中获取tapped链接的url.链接采用自定义格式,可触发应用中的某些操作.例如HTTP://我的网站/帮助#深层链接对讲.我这样使用KVO:这在第一次点击链接时效果很好.但是,如果我连续两次点击相同的链接,它将不报告链接点击.是否有解决方法来解决这个问题,以便我可以检测每个点击并获取链接?任何关于这个的指针都会很棒!解决方法像这样更改addobserver在observeValue函数中,您可以获得两个值

  10. ios – 在Swift的UIView中找到UILabel

    我正在尝试在我的UIViewControllers的超级视图中找到我的UILabels.这是我的代码:这是在Objective-C中推荐的方式,但是在Swift中我只得到UIViews和CALayer.我肯定在提供给这个方法的视图中有UILabel.我错过了什么?我的UIViewController中的调用:解决方法使用函数式编程概念可以更轻松地实现这一目标.

返回
顶部