【问题标题】:How to clear the content of subview before drawing绘制前如何清除子视图的内容
【发布时间】:2012-04-14 04:40:05
【问题描述】:

我使用两个块在我的子视图上动态绘制一些按钮。一个计算纵向模式的帧数,另一个计算横向模式的帧数。它运作良好,但当我旋转时,它会覆盖旧的。因此,我的一些按钮来了两次。这是我检测方向的代码:

//i have defined blocks in viewDidLoad: and everything is ok till here
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
        {
            dispatch_async(dispatch_get_main_queue(), PortraitBlock);        
        }       
        else 
        { 
            dispatch_async(dispatch_get_main_queue(), LandscapeBlock);
               }
        return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || 
                interfaceOrientation == UIInterfaceOrientationPortrait || 
                interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
                interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
    }

现在,如何清理添加按钮的视图?

注意:我在 UIView 对象上添加按钮,并且该对象也在 UIScrollView 对象上

【问题讨论】:

  • @illis 如果按钮被添加两次然后给按钮标签并检查按钮是否存在然后不要添加它
  • 您无需在每次旋转设备时重新绘制按钮。只需将它们移动到适当的位置并根据需要更改外观即可。
  • 我需要有关您的特殊情况的更多信息才能提供帮助。
  • @SVGreg 谢谢,我解决了

标签: ios ipad uiview uibutton


【解决方案1】:

在从视图中删除视图之前,首先检查类,然后将其删除。之后,您可以根据方向在屏幕上添加按钮。

for(UIView *view in [View to add buttons].subviews)
{
   if ([view isKindOfClass:[UIButton class]]) [view removeFromSuperview];
}

如果您使用任何customButton,请提及您的customButtonClassName 代替UIButton

【讨论】:

    【解决方案2】:

    嗨,.,

    使用您在视图上分配的所有按钮尝试以下代码 将被删除。

    for(UIButton *view in yourview.subviews)
        {
            [view removeFromSuperview];
        }
    

    【讨论】:

    • 我确实在块中尝试过这个,后来在旋转方法上尝试过。但它删除了所有的滚动视图。出现一个空白屏幕
    • 这将从您的视图中删除所有子视图。
    • 用一些标签值标记按钮的视图,以便找到它们。
    【解决方案3】:

    根本不要添加新按钮。只需更换旧框架即可。

    如果您需要添加新按钮。只是删除旧的!?要删除所有子视图,您可以使用:

    for(UIView* view in self.view.subviews)
    {
        [view removeFromSuperview];
    }
    

    【讨论】:

    • 任何 UI 更改都必须在主线程上完成,为什么要异步调用这些块?什么是崩溃!?有日志吗?
    • 我在我的块中尝试了您的代码,但出现了一个空白屏幕。没有崩溃但也没有结果
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 2011-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-09
    相关资源
    最近更新 更多