推送 UIViewController 后 UIView 不居中

UIView not centered after pushing UIViewController

我在我的项目中使用 objective-c。我想显示一个带有导航栏的新 UIViewController(例如 A)这是我的代码:

AViewController *AVC = [[AViewController alloc]init];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController: AVC];
nav.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:nav animated:YES completion:nil];

之后,当用户点击特定的 UIButton 时,我会使用以下代码推送一个新的 UIViewController(例如 B):

BViewController *BVC = [[BViewController alloc]init];
[self.navigationController pushViewController: BVC animated:YES];

inside B viewController 我想在屏幕中央显示一个 UIView,这是我的代码:

UIView *someView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
someView.backgroundColor = [UIColor redColor];

CGFloat x = self.view.center.x;
CGFloat y = self.view.center.y;

someView.center = CGPointMake(x, y);

[self.view addSubview:someView];

这是我的问题,someView 没有显示在设备中心,为什么会发生这种情况以及如何解决我的问题?

因为您的控制器视图在 viewDidLoad 上没有获得正确的框架。请将 autoresizingMask 属性 添加到 someView:

someView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;