ios – 如何使子视图居中

ios – 如何使子视图居中,第1张

概述我正在向另一个UIViewController添加一个UIViewController子视图. 它很棒.但我很难尝试将子视图置于父视图的中间. 我读了,我找到了两行代码,但对我来说不起作用. 谁能指出我的问题? 那些是: popupController.view.center = [self.view convertPoint:self.view.center fromView:self.view 我正在向另一个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];}

儿童设置

解决方法 看起来像是在居中后重新定位视图控制器视图.可能在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;}

可能你需要稍微修改一下这段代码.但我确定你的问题是不正确的(执行时间)放置中心代码 – 随后被其他一些视图定位覆盖.

总结

以上是内存溢出为你收集整理的ios – 如何使子视图居中全部内容,希望文章能够帮你解决ios – 如何使子视图居中所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1039973.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存