【问题标题】:How to remove View from superView?如何从超级视图中删除视图?
【发布时间】:2014-05-02 12:49:42
【问题描述】:

我创建了一个xib,并通过以下代码将其显示在视图中。

    NSArray *xibContents = 
        [[NSBundle mainBundle] loadNibNamed:@"PickupAddressView"
            owner:self 
            options:nil];

    UIView *view = [xibContents objectAtIndex:0]; // Safer than objectAtIndex:0
    //UIView *subView=[view.subviews objectAtIndex:0];

    [view setFrame:CGRectMake(0.0f, 0.0f, 0.0f, 0.0f)];
    CGRect tempFrame=view.frame;
    tempFrame.size.width=300;//change acco. how much you want to expand
    tempFrame.size.height=350;
    [UIView beginAnimations:@"" context:nil];
    [UIView setAnimationDuration:0.2];
    view.frame=tempFrame;
    //[UIView commitAnimations];
    [view setFrame:CGRectMake(10.0f, 90.0f, 300.0f, 350.0f)];
    [view setBackgroundColor:
      [UIColor colorWithPatternImage:
        [UIImage imageNamed:@"Register_bg"]]];
    [UIView commitAnimations];

    [view removeFromSuperview];
    [self.view addSubview:view];

我正在使用UIStoryboard,并制作了这个xib,并从以下代码中显示。

如何删除此视图。我有一个UIButton,正在调用它的IBAction

谢谢

【问题讨论】:

标签: ios objective-c storyboard xib superview


【解决方案1】:

保持对视图的弱引用(属性或实例变量)并像这样调用removeFromSuperview

[self.viewToRemove removeFromSuperview];

【讨论】:

    【解决方案2】:

    从超级视图中删除任何视图太简单了,只需键入

    [yourview removeFromSuperview];
    

    如果您将视图声明为属性,则使用如下所示。

    [self.yourview removeFromSuperview];
    

    【讨论】:

    • 我没有给你投反对票。如果您知道答案,请看这个问题,如果您的答案已经由某人提供,请给他们投票或离开。或添加更多观点并作为答案发布
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多