如何从滚动视图中删除子视图?

时间:2022-11-23
本文介绍了如何从滚动视图中删除子视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从我的滚动视图中删除所有子视图...

how do i remove all subviews from my scrollview...

我在滚动视图中有一个 uiview 和它上方的按钮,如下所示....

i have a uiview and a button above it in the scrollview something like this....

这是我在滚动视图中添加子视图的代码

here is my code to add subview in scroll view

-(void)AddOneButton:(NSInteger)myButtonTag {
lastButtonNumber = lastButtonNumber + 1;

if ((lastButtonNumber == 1) || ((lastButtonNumber%2) == 1)) {
btnLeft = 8;}
else if ((lastButtonNumber == 2) || ((lastButtonNumber%2) == 0)) {
btnLeft = 162;
}
CGRect frame1 = CGRectMake(btnLeft, btnTop, 150, 150);
CGRect frame2 = CGRectMake(btnLeft, btnTop, 150, 150);
UIButton *Button = [UIButton buttonWithType:UIButtonTypeCustom];
Button.frame = frame1;
Button.tag = myButtonTag;
[Button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[Button setBackgroundColor:[UIColor clearColor]];
[Button setBackgroundImage:[UIImage imageNamed:@"WaitScreen.png"] forState:UIControlStateHighlighted];

    GraphThumbViewControllerobj = [[GraphThumbViewController alloc] initWithPageNumber:[[GraphIdArray objectAtIndex:myButtonTag]intValue]];
    GraphThumbViewControllerobj.view.frame=frame2;
    GraphThumbViewControllerobj.lblCounter.text=[NSString stringWithFormat:@"%d of %d",myButtonTag+1,flashCardsId.count];
    GraphThumbViewControllerobj.lblQuestion.text=[flashCardText objectAtIndex:myButtonTag];
    [myScrollView addSubview:GraphThumbViewControllerobj.view];


[myScrollView addSubview:Button];


if ((lastButtonNumber == 2) || ((lastButtonNumber%2) == 0)) {
btnTop = btnTop + 162;
}
if (btnTop+150 > myScrollView.frame.size.height) {
myScrollView.contentSize = CGSizeMake((myScrollView.frame.size.width), (btnTop+160));}
}

这是删除子视图的代码

if(myScrollView!=nil)
{
        while ([myScrollView.subviews count] > 0) {
            //NSLog(@"subviews Count=%d",[[myScrollView subviews]count]);
            [[[myScrollView subviews] objectAtIndex:0] removeFromSuperview];
}

推荐答案

要从任何视图中删除所有子视图,您可以遍历子视图并为每个子视图发送 removeFromSuperview 调用:

To remove all the subviews from any view, you can iterate over the subviews and send each a removeFromSuperview call:

// With some valid UIView *view:
for(UIView *subview in [view subviews]) {
    [subview removeFromSuperview];
}

不过,这完全是无条件的,并且会删除给定视图中的所有子视图.如果你想要更细粒度的东西,你可以采取几种不同的方法:

This is entirely unconditional, though, and will get rid of all subviews in the given view. If you want something more fine-grained, you could take any of several different approaches:

  • 维护您自己的不同类型的视图数组,以便稍后以相同的方式向它们发送 removeFromSuperview 消息
  • 保留您创建它们的所有视图并保留指向这些视图的指针,以便您可以根据需要单独发送它们 removeFromSuperview
  • 在上述循环中添加 if 语句,检查类是否相等.例如,要仅删除视图中存在的所有 UIButton(或 UIButton 的自定义子类),您可以使用以下内容:
  • Maintain your own arrays of views of different types so you can send them removeFromSuperview messages later in the same manner
  • Retain all your views where you create them and hold on to pointers to those views, so you can send them removeFromSuperview individually as necessary
  • Add an if statement to the above loop, checking for class equality. For example, to only remove all the UIButtons (or custom subclasses of UIButton) that exist in a view, you could use something like:
// Again, valid UIView *view:
for(UIView *subview in [view subviews]) {
    if([subview isKindOfClass:[UIButton class]]) {
        [subview removeFromSuperview];
    } else {
        // Do nothing - not a UIButton or subclass instance
    }
}

这篇关于如何从滚动视图中删除子视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:如何禁用 UIScrollView 的水平滚动? 下一篇:IOS:在滚动视图中添加图像视图以进行缩放

相关文章

最新文章