0

viewFromIB在 Interface Builder 中创建了一个 UIView,我们称之为它,并在我的 ViewController 加载时隐藏了它。我正在创建另一个 UIView ,让我们modalViewType通过代码调用它并将其添加为子视图self.navigationController.view

UIView *modalViewType = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
modalViewType.opaque = NO;
modalViewType.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6f];
[self.navigationController.view addSubview:modalViewType];

现在我正在取消隐藏viewFromIB,添加viewFromIB为 subViewmodalViewTypeviewFromIB使用bringSubviewToFront:方法将其置于前面。但viewFromIB不是变得可见,虽然我能看到modalViewType。这是代码:

viewFromIB.hidden = NO;
viewFromIB.layer.masksToBounds = YES;
viewFromIB.center = CGPointMake(modalViewType.frame.size.width/2, modalViewType.frame.size.height/2);
[modalViewType addSubview:viewFromIB];
[modalViewType bringSubviewToFront:viewFromIB];  

仅供参考,这曾经在 iOS6 中运行良好,但现在在 iOS7 中大发雷霆。如果需要任何其他信息,请告诉我。非常感谢。

更新:如果我不使用modalViewTypeUIView 而只是取消隐藏viewFromIB它就bringSubviewToFront:可以self.view正常工作。仅当我尝试添加modalViewType. 我的 subView 的框架似乎完全合乎逻辑。

4

2 回答 2

0

I realized that this line of code:

viewFromIB.layer.masksToBounds = YES;

was causing the trouble. masksToBounds method indicates whether sublayers are clipped to the layer’s bounds. To my knowledge this shouldn't have any impact on UIView's own visibility. I tried to find root cause of the issue but could not :( Anyways, just wanted to share that removing this line sorted out my issue and now my subview is visible. FYI: using this line in iOS6 never had any adverse impact on visibility of UIView.

于 2013-11-06T07:08:49.520 回答
0

如果viewFromIB来自 Interface Builder,它可能已经添加到某个超级视图中。我不确定addSubview:已作为子视图添加到另一个视图的视图的定义行为是什么。

也许你可以removeFromSuperview先打电话试试。

此外,请检查您的大小viewFromIB- iOS7 会以意想不到的方式调整大小。

于 2013-10-30T09:56:55.977 回答