sendSubviewToBack和insertSubview的问题:belowSubview:不能正常工作

时间:2012-06-14 16:33:11

标签: iphone objective-c ios cocoa-touch

我遇到sendSubviewToBackinsertSubview:belowSubview:方法的问题。简单地说,我有一个按钮,我正在另一个视图_centerView下面插入。我希望按钮保持在_centerView以下。

我面临的问题是,当我在_centerView下面插入一个按钮时,我会在_centerView上看到一个闪烁(只是片刻)。我尝试了sendSubviewToBack:insertSubview:belowSubview: - 效果相同。

你有什么想法可能有什么不对吗?我错过了什么吗?这是我的代码:

    UIButton* itemButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [itemButton setFrame:CGRectMake(0, 0, 40, 40)];
    [itemButton setCenter:_centerView.center];

    [parentView bringSubviewToFront:_centerView];
    [parentView insertSubview:itemButton belowSubview:_centerView];

3 个答案:

答案 0 :(得分:2)

我最近遇到了同样的问题,但将此选项传递给动画代码修复了它。希望这对你也有帮助。如果没有你的评论,我不会想出我的解决方案,所以,谢谢你:)

UIViewAnimationOptionShowHideTransitionViews

答案 1 :(得分:0)

我不确定insertSubview背后的内部结构是什么,但是如果你想摆脱闪烁,最简单的方法是在插入之前隐藏视图并在之后再次显示它。 / p>

[itemButton setHidden:YES];
[parentView insertSubview:itemButton belowSubview:_centerView];
[itemButton setHidden:NO];

不是最优雅的解决方案,但它应该完成工作。

答案 2 :(得分:0)

好的,所以过了一会儿结果发现这是窗口的错误我动画的同时我添加了按钮。我不知道为什么但是窗口动画的淡入淡出导致了这个问题。我切换了UIWindow的动画,现在它可以工作了。谢谢你的帮助!