设置子UIViews的帧大小

时间:2014-07-29 15:11:57

标签: ios objective-c ipad uiview uiviewcontroller

我有一个UIViewController,我将其称为parentViewController(它本身位于标签视图控制器中,但我不认为与此问题相关)

我试图让这个UIViewController在分屏设置中显示两个子视图。其中一个子视图由UITableViewController的自定义子类控制(称之为tvc。另一个子视图由自定义UIViewController控制,称之为dvc

init的{​​{1}}方法中,我实例化了两个子视图控制器,parentViewControllertvc

dvc的{​​{1}}方法中,我添加了子视图控制器和子视图。

如果我只添加 viewDidLoad子项,它可以正常工作:表视图占用所有可用空间,正确旋转等等。这就是代码的样子:

parentViewController

但是,如果我然后添加tvc控制器和子视图,它将停止正常工作。表视图不显示,只显示空白白色视图。这是我添加- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. _tvc.view.translatesAutoresizingMaskIntoConstraints = NO; [self addChildViewController:_tvc]; [self.view addSubview:_tvc.view]; NSDictionary *viewDictionary = @{ @"tvc": _tvc.view }; NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|[tvc]|" options:0 metrics:nil views:viewDictionary]; [self.view addConstraints:constraints]; } 时的外观:

dvc

我没有用框架初始化dvc,只是让超类- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. _tvc.view.translatesAutoresizingMaskIntoConstraints = NO; [self addChildViewController:_tvc]; [self.view addSubview:_tvc.view]; _dvc.view.translatesAutoresizingMaskIntoConstraints = NO; [self addChildViewController:_dvc]; [self.view addSubview:_dvc.view]; NSDictionary *viewDictionary = @{ @"_tvc": _tvc.view, @"_dvc": _dvc.view }; NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|[_tvc]-[_dvc]|" options:0 metrics:nil views:viewDictionary]; [self.view addConstraints:constraints]; } 处理它。在tvc UITableViewController方法中,我使用dvc框架实例化自定义UIView,因为我假设自动布局约束会正确设置大小。

我做错了什么,我不能同时显示两个孩子的观点?

2 个答案:

答案 0 :(得分:0)

我有类似的情况,因为添加子控制器后没有任何限制。

我添加了用作子视图控制器容器的视图。这是代码,我用来添加控制器:

[self addChildViewController:controller];
// menu view is container for menu controller subview (in your case tvc)
[self.menuView addSubview:controller.view];
// add constraints
[self pinView:controller.view toSuperView:self.menuView];
[controller didMoveToParentViewController:self.menuController];

以下函数使子视图填充父视图

- (void) pinView:(UIView *) childView toSuperView:(UIView *) superView {
    UIView *addedSubview = childView;
    addedSubview.translatesAutoresizingMaskIntoConstraints = NO;

    [superView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[addedSubview]|" options:nil metrics:nil views:NSDictionaryOfVariableBindings(addedSubview)]];
    [superView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[addedSubview]|" options:nil metrics:nil views:NSDictionaryOfVariableBindings(addedSubview)]];
}

答案 1 :(得分:0)

试一试。

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

    _tvc.view.translatesAutoresizingMaskIntoConstraints = NO;
    [self addChildViewController:_tvc];
    [self.view addSubview:_tvc.view];

    _dvc.view.translatesAutoresizingMaskIntoConstraints = NO;
    [self addChildViewController:_dvc];
    [self.view addSubview:_dvc.view];


    // For constraints
    NSMutableArray *allContraints = [[NSMutableArray alloc] init];
    CGFloat halfScreen = [UIScreen mainScreen].bounds.size.height / 2.0;

    NSDictionary *metrics = @{
                              @"viewHeight": @(halfScreen)
                              };

    NSDictionary *viewDictionary = @{ @"tvc": _tvc.view, @"dvc": _dvc.view};
    NSArray *constraintsVerticals = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[tvc(viewHeight)]-[dvc]|" options:0 metrics:metrics views:viewDictionary];
    [allContraints addObjectsFromArray:constraintsVerticals];

    NSArray *constraintsTvcHorz = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[tvc]|" options:0 metrics:nil views:viewDictionary];
    [allContraints addObjectsFromArray:constraintsTvcHorz];

    NSArray *constraintsDvcHorz = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[dvc]|" options:0 metrics:nil views:viewDictionary];
    [allContraints addObjectsFromArray:constraintsDvcHorz];


    [self.view addConstraints:allContraints];

}