xcode iphone:以编程方式展开和折叠 UIView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5151250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:43:25  来源:igfitidea点击:

iphone: expand and collapse a UIView programmatically

iphonexcodeipad

提问by Ankit Sachan

I am very new to iPhone/iPad development. can you please help me create this programmatically. I want to expand/collapse a UIView programmatically.

我对 iPhone/iPad 开发很陌生。你能帮我以编程方式创建这个吗?我想以编程方式展开/折叠 UIView。

This expandable/collapsable view will have some text field and lables which should appear and disappear with that view

此可展开/可折叠视图将有一些文本字段和标签,它们应该随该视图一起出现和消失

回答by Dee

Suppose you have a UIView instance in the UIViewController class like this:

假设您在 UIViewController 类中有一个 UIView 实例,如下所示:

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y, w1, h1)];
[self.view addSubview:view];

based on the requirement you set the view visibility as I did here .. I am not displaying the view when the controller is loading it's view ..

根据您设置视图可见性的要求,就像我在这里所做的一样..当控制器加载它的视图时,我没有显示视图..

[view setHidden:YES];

Maintain a flag to check the visibility of the view instance .. lets say isViewVisibleis my flag to check the visibility of the view .. I set it to NO in the begning ..

维护一个标志来检查视图实例的可见性..让isViewVisible我说是我检查视图可见性的标志..我在开始时将它设置为 NO..

isHelpViewVisible = NO;

and I wrote an action method (viewClicked) here to expand and to collapse the view object , give this action method to a button instance and it will work.

我在这里写了一个动作方法 (viewClicked) 来展开和折叠视图对象,将这个动作方法赋予一个按钮实例,它就会起作用。

- (void)viewClicked:(id)sender {

    if (!isViewVisible) {
        isViewVisible = YES;
        [view setHidden:NO];
        [UIView beginAnimations:@"animationOff" context:NULL]; 
        [UIView setAnimationDuration:1.3f];
        [view setFrame:CGRectMake(x, y, w1, h1)];
        [UIView commitAnimations];
    } else {
        isViewVisible = NO;
        [view setHidden:NO];
        [UIView beginAnimations:@"animationOff" context:NULL]; 
        [UIView setAnimationDuration:1.3f];
        [view setFrame:CGRectMake(x, y, width, hight)];
        [UIView commitAnimations];
    }

}

and add the textfields and labels objects to the view object as subviews and set the animation to those objects as well .. it will work.

并将文本字段和标签对象作为子视图添加到视图对象,并将动画设置为这些对象......它会起作用。

回答by Danoli3

Replaces the following from above:

从上面替换以下内容:

[UIView beginAnimations:@"animationOff" context:NULL];
[UIView setAnimationDuration:1.3f];
[view setFrame:CGRectMake(x, y, w1, h1)];
[UIView commitAnimations];

With the New way of doing animations:

使用新的动画制作方式:

[UIView animateWithDuration:1.3f animations:^{
    self.frame = CGRectMake( x1, x2, w1, h1);
} completion:^(BOOL finished) {
    // this gives us a nice callback when it finishes the animation :)
}];

回答by Bogatyr

To change the size / position of a UIView within its parent just change its frame property:

要在其父级中更改 UIView 的大小/位置,只需更改其 frame 属性:

CGRect newFrame = myView.frame;
newFrame.origin.x = newX;
newFrame.origin.y = newY;
newFrame.size.width = newWidth;
newFrame.size.height = newHeight;
myView.frame = newFrame;

回答by SNR

You can try this

你可以试试这个

CGRect frame = [currentView frame];
frame.size.width = 999;//Some value
frame.size.height = 444;//some value
[currentView setFrame:frame];
每当您想增加/减少视图大小时,您都可以遵循此操作