gpt4 book ai didi

ios - 如何居中 subview

转载 作者:可可西里 更新时间:2023-11-01 05:45:19 24 4
gpt4 key购买 nike

我正在将一个 UIViewController subview 添加到另一个 UIViewController。

效果很好。但是我很难尝试将 subview 居中放置在父 View 的中间。

我仔细阅读,发现有 2 行代码对其他人有用,但对我不起作用。

谁能指出我的问题??

它们是:

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);

父 View Controller 代码:

- (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];


}

enter image description here

子设置 enter image description here enter image description here

最佳答案

似乎您在将 View Controller View 居中后重新定位了它。可能在 didMoveToParentViewController: 中。

将居中代码移到selectRoutine:方法的末尾

- (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];

}

或者更好的是,将其移动到 didMoveToParentViewController:

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

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

您可能需要稍微修改一下这段代码。但我确定您的问题是居中代码的不正确(执行时间)放置 - 随后被其他一些 View 定位覆盖。

关于ios - 如何居中 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16373640/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com