Resizing UIView with UIToolbar do not resize simultaneously - ios

I have created a UIView with a UIToolbar both programmatically and with IB. I try to resize the UIView but the tool bar resizes first and make it obvious that there are 2 views. Can anyone tell me how to resize both view simultaneously so that it give the appearance that the UIView and toolbar are one? I have set all the appropriate masks. The code below is called on the UIView from the UIView class.
Thanks!!!
[UIView animateWithDuration: .3 animations:^ {
CGRect frame = self.frame;
frame.origin.x = 0;
frame.origin.y = 20;
frame.size.height = 900;
frame.size.width = 768;
self.frame = frame;
}];

Animate both at the same time:
[UIView animateWithDuration: .3 animations:^ {
toolbar.frame = CGRectMake(0.0, 20.0, 900.0, 768.0);
self.frame = CGRectMake(0.0, 20.0, 900.0, 768.0);
}];
Or use some NSLayoutConstraint to achieve equal frames.

Related

How to animate changes in UIButton content alignment

Several of UIButton's properties are not inherently animatable. One of these properties is the contentHorizontalAlignment property. After searching SO with no luck, I came up with the answer below to solve the "not-animatable" issue.
You should animate layoutIfNeeded after change alignment value:
Objective C:
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[UIView animateWithDuration:0.3 animations:^{
[self.view layoutIfNeeded];
}];
Swift3:
button.contentHorizontalAlignment = .left
UIView.animate(withDuration: 0.3, animations: {
self.view.layoutIfNeeded()
})
This method first animates UIButton's titleLabel frame manually, then sets the content alignment and content edge insets to preserve UIButton default behavior.
CGRect frame = button.titleLabel.frame;
frame.origin.x = 10;
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
button.titleLabel.frame = frame;
} completion:^(BOOL finished) {
button.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
}];

Resizing UIView with animation, subview won't animate

Using UIView's animateWithDuration:animations:completion:, I'm resizing a UIView and a subview of that UIView which is a subclass of UITableView.
The UIView resizes fine, but the UITableView doesn't. It does move around a little, but the frame does not update properly and reverts to its original state.
Edit: if I move the resizing to the completion block.... it works. What gives?
tweetTable.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[tweetTable endUpdates];
[UIView animateWithDuration:DURATION animations:^{
CGRect leftFrame = leftPane.frame;
leftFrame.size.width = self.view.frame.size.width - MARGIN;
leftPane.frame = leftFrame;
leftPaneButton.frame = leftFrame;
CGRect tweetFrame = tweetTable.frame;
tweetFrame.size.width = leftPane.frame.size.width;
NSLog(#"%f to %f", tweetTable.frame.size.width, leftPane.frame.size.width);
tweetTable.frame = tweetFrame;
tweetTable.backgroundColor = [UIColor redColor];
tweetTable.alpha = 0.5f;
sideInfo.center = CGPointMake(self.view.frame.size.width - MARGIN + (sideInfo.frame.size.width / 2), sideInfo.center.y);
rightPaneButton.center = sideInfo.center;
} completion:^(BOOL finished) {
leftExtended = TRUE;
[tweetTable beginUpdates];
}];
Check in your storyboard if the UIView has Autoresize Subviews checked. That means that it resizes all of its subviews when the view itself gets resized. That would explain why it works in the completion block.

UIView animation for multi views

I am trying to make animation for two views like this:
CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI_2);
CGRect rect = self.oneView.frame;
rect.origin.y = 10;
[UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
self.oneView.frame = rect;
self.buttonView.transform = transform;
} completion:^(BOOL finished) {
...
}];
But only buttonView has rotated. OneView stays in place. But if I comment
//self.buttonView.transform = transform;
OneView has moved.
rect.origin.y = 10; is probably giving you a warning because you can't write directly to a CGRect.
You'll have to do rect = CGRectMake(rect.origin.x, 10.0f, rect.size.width, rect.size.height).
What is the relationship between oneView and buttonView? If oneView is a child of buttonView, then this will be an expected effect.
Have you tried to run these animations from 2 separate calls?

iOS - Interface elements overlapping when move the view

I have the following interface layout on my application:
When I click on a text field (if you lick add tag a new text field is added) I make my view scroll up as to not be obstructed by the keyboard so the user can properly see what he's typing, but the following happens when I do so:
This is the code I use to scroll:
- (void)keyboardWillShow:(NSNotification *)notification {
if (self.keyboardIsShown) {
return;
}
NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGSize keyboardSize = [keyboardBoundsValue CGRectValue].size;
NSTimeInterval animationDuration = 0.3;
CGRect frame = self.view.frame;
frame.origin.y -= keyboardSize.height - 94 + self.firstResponder.superview.superview.frame.origin.y;
frame.size.height += keyboardSize.height - 94 + self.firstResponder.superview.superview.frame.origin.y;
[UIView beginAnimations:#"ResizeForKeyboard" context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
self.keyboardIsShown = YES;
}
- (void)keyboardWillHide:(NSNotification *)notification {
NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGSize keyboardSize = [keyboardBoundsValue CGRectValue].size;
NSTimeInterval animationDuration = 0.3;
CGRect frame = self.view.frame;
frame.origin.y += keyboardSize.height - 94 + self.firstResponder.superview.superview.frame.origin.y;
frame.size.height -= keyboardSize.height - 94 + self.firstResponder.superview.superview.frame.origin.y;
[UIView beginAnimations:#"ResizeForKeyboard" context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
self.keyboardIsShown = NO;
}
Any idea how I can make the view elements show behind the navbar or disappear or another workaround that would work properly?
A quick way to achieve what you want, would be to have your navigation bar below the "content"-view in your view tree. That way the navigation bar will remain on top. If you add the content dynamically. Make sure you add them as subviews of a view that is behind the navigation bar.
// Edit
As proposed in the comments:
self.view.clipsToBounds = YES;
CGRect frame = self.yourWrappingView.frame;
NSUInteger distanceToMove = 200;
self.yourWrappingView.frame = CGRectMake(frame.origin.x - distanceToMove, frame.origin.y, frame.size.width, frame.size.height);
This should work for you mate :)
Probably you are not using a real UINavigationController. But you should. So you wouldn't have those problems and a correct resizing behaviour. But to solve it in your current architecture:
Make sure the Navigation Bar is the top most object in your nib file / storyboard
add an IBOutlet for your UINavigationBar and if you insert new views dynamically, use [self insertSubview: newView belowSubview: navigationBarOutlet];

unclear resize UIWebView behavior

I am trying to resize a UIWebView like this:
CGRect oldFrame = zoomView.frame;
zoomView.layer.anchorPoint = CGPointMake(1, 0.5);
zoomView.frame = oldFrame;
if (oldFrame.size.width == 1024) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
zoomView.frame = CGRectMake(512, 0, 512, 470);
[UIView commitAnimations];
} else {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
zoomView.frame = CGRectMake(0, 0, 1024, 470);
[UIView commitAnimations];
}
The resize works, but the content of the Webview won't follow the resize. It looks like the content get set to the new bounds first and then the View resizes. In the case from 1024 width to 512, the content get set to 512 width directly and then the view shrinks to its new size and position.
What do I oversee here?
Seems it this is the way how animating size and origin get animated. The scrollview won't get updated during this. Solved it by using an own timer and move the view myself.

Resources