gpt4 book ai didi

ios 动态改变 View 的大小

转载 作者:行者123 更新时间:2023-11-28 21:52:13 25 4
gpt4 key购买 nike

如何在 iOS 中以编程方式更改 View 的大小。

我正在使用 Objective C 开发一个 iOS 应用。

我有几个 View ,它们使用以下方法根据特定逻辑隐藏和显示:

-(void)hideGroupStats{
[groupTimeToFinishForeText setHidden:YES];
[lblTimeToFinish setHidden:YES];
[groupTimeToFinishBG setHidden:YES];
}

在我的 View 层次结构中,在它们下面有一个 View ,其中包含一张带有“缩放以适合模式”的 map 。然而,当其他 View 被隐藏时,它不会调整大小以占用空间。

我是 ios 开发的新手,所以我可能会错过一些非常简单的事情。

最佳答案

根据 Apple documentation for UIView (请参阅下面的摘录)当调用 setHidden:YES 时,这实际上并没有从它的 super View 中删除 View ,它只是让它消失,所以它等效地仍然存在,你只是看不到它。有几种方法可以隐藏 View ,这样你就可以得到你想要的效果,但我最主要的方法是改变 UIView 的框架(比如 David Ansermots as says given +1)

mapView.frame = CGRectMake(newX, newY, newWidth, newHeight); 

newXnewYnewWidthnewHeight 都是您在某处设置的变量,用于确定大小和 UIView 的位置。但是我会将这条线放在某种动画中,以便为用户提供更好的用户体验,所以我个人会做类似的事情:

- (void)showView:(UIView *)view withAnimationForFrame:(CGRect)frame 
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];

view.frame = CGRectMake(frame);

[UIView commitAnimations];
}

- (void)hideView:(UIView *)view withAnimationForFrame:(CGRect)frame
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

view.frame = CGRectMake(frame);

[UIView commitAnimations];
}

更改框架大小和/或位置应使您想要缩放的其他 View 自动按照您的需要进行缩放。

Extract from Apple Documentation for UIView

Setting the value of this property to YES hides the receiver and setting it to NO shows the receiver. The default value is NO.

A hidden view disappears from its window and does not receive input events. It remains in its superview’s list of subviews, however, and participates in autoresizing as usual. Hiding a view with subviews has the effect of hiding those subviews and any view descendants they might have. This effect is implicit and does not alter the hidden state of the receiver’s descendants.

Hiding the view that is the window’s current first responder causes the view’s next valid key view to become the new first responder.

The value of this property reflects the state of the receiver only and does not account for the state of the receiver’s ancestors in the view hierarchy. Thus this property can be NO but the receiver may still be hidden if an ancestor is hidden.

关于ios 动态改变 View 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27902041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com