4

我正在将 UIViewController 子视图添加到另一个 UIViewController。

它工作得很好。但是我很难将子视图置于父视图中间。

我读了起来,发现有两行代码对其他人有用,但对我不起作用。

谁能指出我的问题??

那些是:

popupController.view.center = [self.view convertPoint:self.view.center fromView:self.view.superview];

popupController.view.center = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.size.height / 2);

父视图控制器代码:

- (IBAction)selectRoutine:(id)sender {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];

    createRoutinePopupViewController* popupController = [storyboard instantiateViewControllerWithIdentifier:@"createRoutinePopupView"];
   // popupController.view.center = [self.view convertPoint:self.view.center fromView:self.view.superview];
    popupController.view.center = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.size.height / 2);

    //Tell the operating system the CreateRoutine view controller
    //is becoming a child:
    [self addChildViewController:popupController];

    //add the target frame to self's view:
    [self.view addSubview:popupController.view];

    //Tell the operating system the view controller has moved:
    [popupController didMoveToParentViewController:self];


}

在此处输入图像描述

子设置 在此处输入图像描述 在此处输入图像描述

4

3 回答 3

2

Seems like you reposition your view controllers view after centering it. Probably in didMoveToParentViewController:.

Move the centering code to the end of selectRoutine: method

- (IBAction)selectRoutine:(id)sender
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];

    createRoutinePopupViewController* popupController = [storyboard instantiateViewControllerWithIdentifier:@"createRoutinePopupView"];

    [self addChildViewController:popupController];
    [self.view addSubview:popupController.view];
    [popupController didMoveToParentViewController:self];

    //do the centering here
    popupController.view.center = [self.view convertPoint:self.view.center fromView:self.view.superview];

}

Or better yet, move it to the didMoveToParentViewController:

- (void)didMoveToParentViewController:(UIViewController *)parent
{
    //whatever code you have here

    self.view.center = self.view.superview.center;
}

Possibly you will have to modify this code a bit. But i'm certain that your problem is incorrect (execution-time-wise) placement of the centering code - that gets subsequently overriden by some other view-positioning.

于 2013-05-04T12:41:06.900 回答
1

为了将子视图移动到视图中心,我尝试了这个

YourView.center = CGPointMake(self.view.center.x,self.view.center.y);
于 2016-05-19T08:34:18.903 回答
0

此处发布的解决方案对我有用。通常,如果您希望微调器位于 UIAlertController 消息(通常位于 UIAlertController 的中心)和下方的任何操作按钮(如确定或取消)之间,请稍微调整它的高度,例如

spinnerIndicator.center = CGPointMake(self.superview.view.bounds.width / 2,
            (self.alertVC.view.bounds.height / 2) * 1.07)
// The superview of the spinnerIndictator here is the UIAlertController. 

根据我的实验,比率 1.07 将微调器定位在所有可用 iPhone 屏幕下方的消息和操作按钮之间。

于 2016-06-08T07:10:48.987 回答