behrad
behrad

Reputation: 646

UIView not centered after pushing UIViewController

I'm using objective-c in my project. I want to show a new UIViewController (for example A) with navigation bar this is my codes:

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

after that when user tap on a specific UIButton, I push a new UIViewController (for example B) with this codes:

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

inside B viewController I want to show a UIView in center of screen, this is my codes:

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

this is my problem, the someView not showing in center of device, why this is happening and how to resolve my problem?

Upvotes: 1

Views: 144

Answers (1)

sonle
sonle

Reputation: 8959

Because your controller's view did not get the correct frame on viewDidLoad. Please add autoresizingMask property to someView:

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

Upvotes: 3

Related Questions