简体   繁体   English

另一个UIView的UIView子视图,它是UIViewController的子视图,未在iOS中集中到UIViewController

[英]UIView subview of another UIView which is subview of UIViewController, not getting centered to UIViewController in iOS

I have a UIView ( named fwd2) which is added as subview of a UIView (named fwd1) and this UIView is added as a subview to a UIViewController(LoginView Controller) . 我有一个UIView (名为fwd2),它作为UIView (名为fwd1)的子视图添加,并且此UIView作为子视图添加到UIViewController(LoginView Controller) And on the click of of proceed button on fwd1 the fwd2 view is opened.And I'm creating Uiview programitically .UIView (fwd1) is of dynamic height and UIView (Fwd2) is of fixed height. And on the click of of proceed button on fwd1 the fwd2 view is opened.And I'm creating Uiview programitically .UIView (fwd1)具有动态高度, UIView (Fwd2)具有固定高度。 Now I want both the UIView (fwd1 & fwd2) center to the UIViewController (LoginView Controller). 现在,我希望两个UIView (fwd1和fwd2)都位于UIViewController (LoginView Controller)的中心。 My first UIView (fwd1) is getting centered to the UIViewController but second UIView (fwd2) is not getting centered. 我的第一个UIView (fwd1)居中于UIViewController但第二个UIView (fwd2)却未居中。 I have used 我用过

fwd1.center = self.view.center; 

and I have also tried 而且我也尝试过

fwd1.center = [self.view convertPoint:self.view.center 
fromView:self.view.superview]; 

I have done same thing with second UIView ie fwd2 but my second UIView ie fwd2 is not getting centered to UIViewController . 我已经用第二个UIView即fwd2做过同样的事情,但是我的第二个UIView即fwd2没有集中到UIViewController Thanks in advance. 提前致谢。

Have you put the code in the right place? 您将代码放在正确的位置了吗?

These code usually should appear in the viewDidLayoutSubviews of UIViewController. 这些代码通常应出现在UIViewController的viewDidLayoutSubviews中。

Make fwd1 centered in UIViewController: 使fwd1集中在UIViewController中:

fwd1.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));

Then make fwd2 centered in fwd1 , which means fwd2 should also centered in the UIViewController (They have the same center relative to the UIWindow). 然后,让fwd2在中心fwd1 ,这意味着fwd2也应该集中在UIViewController中(它们具有相对于一个UIWindow同一中心)。

fwd2.center = CGPointMake(CGRectGetMidX(fwd1.bounds), CGRectGetMidY(fwd1.bounds));

EDIT: Thanks to the comment, I correct the code. 编辑:感谢注释,我更正了代码。

As per comment of thread creator this, solved his problem: 按照线程创建者的评论,解决了他的问题:

In which function are you writing this code? 您在哪个函数中编写此代码? You might want to try in 您可能想尝试

- viewWillLayoutSubviews

And DO NOT forget to call super's implementation. 并且不要忘记调用super的实现。 Also first you need to center fwd1 and then fwd2. 同样,首先需要将fwd1居中,然后将fwd2居中。

fwd1.center = [self.view convertPoint:self.view.center fromView:self.view.superview]; 
fwd2.center = [fwd1 convertPoint:fwd1.center fromView:fwd1.superview];

Your fwd2 is subview of fwd1 and fwd1 is the subview of UIViewController. 您的fwd2是fwd1的子视图,而fwd1是UIViewController的子视图。 So you need to as below 所以你需要如下

First make centred align the fwd1 to UIViewController 首先使fwd1与UIViewController居中对齐

[fwd1 setCenter:[[self view] center]];

Then make the fwd2 centred align to fwd1 然后使fwd2居中对齐fwd1

[fwd2 setCenter:[fwd1 center]];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM