Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
502 views
in Technique[技术] by (71.8m points)

objective c - 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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...