【问题标题】:Hide all View containing By making height 0 of parent UiView隐藏所有包含通过使父 UiView 的高度为 0 的视图
【发布时间】:2012-10-05 05:15:18
【问题描述】:

我正在制作一个动画,在其中我必须将一个名为“subView”的UIView 的高度设为0。 现在该子视图包含一个UIButtonUIImageView。 现在我通过这种方法在按钮单击时将高度设为 0

@interface ViewController : UIViewController
{
    IBOutlet UIView *suvView;
    IBOutlet UIButton *subButton;
    IBOutlet UIImageView *imgview;
}
-(IBAction)hide:(id)sender;
@end


@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self.view addSubview:suvView];
    [suvView addSubview:subButton];
    [suvView addSubview:imgview];
}

-(IBAction)hide:(id)sender
{
    [UIView beginAnimations:nil context:suvView];
    [UIView setAnimationDuration:0.25];
    [suvView setFrame:CGRectMake(0, 94, 320, 0)];
    [UIView commitAnimations];
}
@end

但是当我将高度设置为 0 时,名为“subButton”和“imgview”的视图看起来是这样的。 我想在 subView 高度为 0 时隐藏它们。

我正在使用 XCode 4.5

【问题讨论】:

    标签: iphone ios xcode ios4 ios6


    【解决方案1】:

    如果你想隐藏它们而不调整大小:[mainview setClipsToBounds:YES]

    【讨论】:

      【解决方案2】:

      你有两种方法可以做到这一点:-

      首先,如果您将 suvViews 高度设为 0,则无需将其移除。但只需隐藏 imgview 和 subButton 之类的

      -(IBAction)hide:(id)sender
      {
          [UIView beginAnimations:nil context:suvView];
          [UIView setAnimationDuration:0.25];
          [suvView setFrame:CGRectMake(0, 94, 320, 0)];
          [UIView commitAnimations];
          subButton.hidden = YES;
          imgview.hidden = YES;
      }
      @end
      

      你还可以做的是降低 subButton 和 imgview 的高度与 suvView 的高度类似

      -(IBAction)hide:(id)sender
      {
          [UIView beginAnimations:nil context:suvView];
          [UIView setAnimationDuration:0.25];
          [suvView setFrame:CGRectMake(0, 94, 320, 0)];
          [imgview setFrame:CGRectMake(0, x, y, z];
          [subButton setFrame:CGRectMake(0, x, y, z];
          [UIView commitAnimations];
      }
      

      其中 x,y,z 是 imgview 和 subButton 的坐标。希望这会有所帮助。

      【讨论】:

        【解决方案3】:
        //try this code for view animation
        
        -(IBAction)hide:(id)sender
        {
        [UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationCurveEaseOut
                         animations:^{
            [suvView setFrame:CGRectMake(0, 94, 320, 0)];
         }
          completion:nil];
        }
        

        【讨论】:

          【解决方案4】:

          在开始动画之前添加以下行:

          subButton.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
          imgView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
          

          或者您也可以从界面构建器中执行此操作,方法是为每个子视图(按钮和图像视图)设置垂直和水平调整大小

          顺便说一句,您应该使用苹果推荐的基于块的动画(如果您使用的是 iOS 4.0 及更高版本)而不是 beginAnimations:commitAnimations:

          【讨论】:

          • 乔纳森的回答是最简单的方法。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-05-02
          • 1970-01-01
          • 2014-11-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多