Reputation: 168
How do I change the size of a view programmatically in iOS.
I am developing an iOS app in Objective C.
I have a couple of views which are hidden and shown according to a certain logic using the following method:
-(void)hideGroupStats{
[groupTimeToFinishForeText setHidden:YES];
[lblTimeToFinish setHidden:YES];
[groupTimeToFinishBG setHidden:YES];
}
below them in my view hierarchy there is a view holding a map with "scale to fit mode". however, when the other views are hidden it doesnt resize to take the space.
i am new to ios development so i could be missing somting very simple.
Upvotes: 0
Views: 2076
Reputation: 183
you can directly set the frame of your view like:-
[yourViewObject SetFrame :CGRectMake(newX, newY, newWidth, newHeight)];
And if you want to set the frame of your view on dependent any other view(map view) then you can simply use :-
yourviewObj.frame = mapview.frame
Hope this will be helpful for you
Upvotes: 0
Reputation: 12123
As per the Apple documentation for UIView
(see extract below) when calling setHidden:YES
this doesn't actually remove the view from it's superview it just makes it disappear so it is equivalently still there you just can't see it. There are a couple of ways to hide the view so you get the affect that you are after but the main one I'd go with is altering the UIView
s frame (like David Ansermot
s as says given +1)
mapView.frame = CGRectMake(newX, newY, newWidth, newHeight);
with newX
, newY
, newWidth
and newHeight
all being variables that you set somewhere to determine the size and location of the UIView
. However I'd stick this line within some sort of animation to give the user a better user experience so I'd personally do something like :
- (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];
}
Changing the frame size and/or location should make the other views that you want to scale do it automatically like you want.
Extract from Apple Documentation for UIView
Setting the value of this property to
YES
hides the receiver and setting it toNO
shows the receiver. The default value isNO
.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.
Upvotes: 1
Reputation: 6112
You have to add code to resize your view like :
mapView.frame = CGRectMake(newX, newY, newWidth, newHeight);
Upvotes: 1