I would like to make this:
So... when a user clicks on the search icon (top-left) the searchBar is set to hidden and UITableView is resized to top. How can I resize a table? Is it possible to achieve this not usign dimensions (like just set table's top to the navigation's bottom or smth)?
Thx.
You have to modify the table view's frame inside an animation block. It is up to you to calculate the correct dimensions of the frame rectangle.
CGRect newFrame = self.tableView.frame;
newFrame.origin.x -= self.navigationBar.frame.size.height;
newFrame.size.height += self.navigationBar.frame.size.height;
[UIView animateWithDuration:0.25 animations:^{
self.tableView.frame = newFrame;
}];
Related
I'm trying to add an animation to a UIImageView when a button is tapped. When the button is tapped I want the imageView to move to the right at the same time as it is increasing in size. I have tried this code, but the imageView aren't animation correct.
- (IBAction)bA:(id)sender {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration:0.5];
CGRect frame = imageView.frame;
frame.origin.x = frame.origin.x + 30;
frame.size.hight = frame.size.hight + 20;
frame.size.width = frame.size.width + 20;
imageView.frame = frame;
[UIView commitAnimations];
}
This code makes the imageView move little to the right, then back again. Wierd behavior, what should I do to animate both size and origin at the same time?
The problem is that you've got Autolayout turned on. With Autolayout, you cannot change the frame of something, because layout comes along and changes it back again.
Either turn Autolayout off, or else animate by changing the constraints of this view instead of its frame.
I want to animate a view inside ViewController, somewhat like the notification center as seen on the iPhone.
How does one extend the subview using the x,y and/or the height values to do this? Because I would like it to be compatible with both the 3.5 inch screens and the 4 inch screens, so it would be better not to use specific values.
I just tested this out in my own project and confirmed that it works on varying screen sizes.
UIView *slidingview = [[UIView alloc] initWithFrame:CGRectOffset(self.view.frame, 0, -self.view.frame.size.height)];
slidingview.backgroundColor = [UIColor whiteColor];
[self.view addSubview:slidingview];
[UIView animateWithDuration:1 animations:^{
slidingview.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
}];
To implement this, simply replace slidingview with the view you want to slide down. You would, however, have to declare your own view using the frame I provided.
How about something like this?
CGRect currentFrame = self.view.bounds;
UIView * slidingView = [UIView alloc] initWithFrame:(CGRect){currentFrame.origin, currentFrame.size.width, 0};
[self.view addSubview:slidingView];
[UIView animateWithDuration:0.5f animations:^{
slidingView.frame = currentFrame;
}];
You may need to use delay version of animateWithDuration... to make animation in effect.
So I have a UIView object (called gauge) on the right hand side that is a vertical bar. I have another label to the left of that bar that I want to display different text at different levels of the vertical bar (navLabel). I want this label to look like it is pinned to some distance (like 10 px) to my vertical bar. My label is left justified. I do this in the different methods where I need to update the label:
CGSize labelSize = [navLabel.text sizeWithFont:[UIFont fontWithName:#"Arial" size:17.0]];
CGRect newFrame = CGRectMake(gauge.frame.origin.x - 10 - labelSize.width, FIRST_HEIGHT, navLabel.frame.size.width, navLabel.frame.size.height);
[UIView animateWithDuration:0.5 animations:^{
self.navLabel.frame = newFrame;
}];
In my different methods, I just change to the appropriate height (i.e. #define FIRST_HEIGHT).
My label ends up in the places I want, but the animation looks funny. For example, if my first label was #"label 1", and my second label was #"my much much much longer label", what happens is the label goes to the correct height, but because the labels are different sizes, you see the x-animation as well which makes it look funny. I only want animation in the y-direction. How can I accomplish that?
I also tried right justified, but then my label does not end up the correct distance to my gauge object. It ends up crossing it many times and I'm not sure why that happens.
It's not clear that this is the behavior you want, but why not just set navLabel.frame to have the correct (new) x-coordinate as its origin before animating, then animate to the new frame that has the correct origin with the new height offset?
Or, set up two animations using these two frames, one that animates the y-direction first and the second that animates the x-direction to the new x-coordinate of the frame origin. Or vice versa.
// This version simply sets the correct origin (and the correct new width), then animates the y-direction
CGSize labelSize = [navLabel.text sizeWithFont:[UIFont fontWithName:#"Arial" size:17.0]];
self.navLabel.frame = CGRectMake(gauge.frame.origin.x-10-labelSize.width, self.navLabel.frame.origin.y,labelSize.width,navLabel.frame.size.height);
// now navLabel's frame has the correct new width (and origin x-coord)
CGRect newFrame = CGRectMake(self.navLabel.origin.x, FIRST_HEIGHT, navLabel.frame.size.width, navLabel.frame.size.height);
// now this animation will cause movement only in the y-direction
[UIView animateWithDuration:0.5 animations:^{
self.navLabel.frame = newFrame;
}];
// This version animates the change in the frame in the x-direction first, then does what it did before
CGSize labelSize = [navLabel.text sizeWithFont:[UIFont fontWithName:#"Arial" size:17.0]];
CGRect newFrame = CGRectMake(gauge.frame.origin.x-10-labelSize.width, self.navLabel.frame.origin.y,labelSize.width,navLabel.frame.size.height);
// this animation will cause movement in the x-direction
[UIView animateWithDuration:0.5 animations:^{
self.navLabel.frame = newFrame;
}];
// now navLabel's frame has the correct new width (and origin x-coord), so set the new y-coord
CGRect newFrame = CGRectMake(self.navLabel.origin.x, FIRST_HEIGHT, navLabel.frame.size.width, navLabel.frame.size.height);
// now this animation will cause movement only in the y-direction
[UIView animateWithDuration:0.5 animations:^{
self.navLabel.frame = newFrame;
}];
My view layout http://i.imgur.com/PkZ9P.png
-(IBAction)tapview:(id)sender
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:1];
CGRect frame = labelgreen.frame;
frame.origin.y -= 200;
labelgreen.frame = frame;
[UIView setAnimationDidStopSelector:#selector(forview1)];
[UIView commitAnimations]; }
but when label move it move only inside view1 or view2 or view 3,
i would like to let it move over all view to color view
what i should do?
and after animation finish it should run function -(void)forview1
but the function is not call
-(void)forview1
{colorview.backgroundcolor=changecolor;}
thank you
Spelling mistake in ur function
NOT
[UIView setAnimationDidStopSelector:#selector(foeview1)];
//correct
[UIView setAnimationDidStopSelector:#selector(forview1)];
more over
replace this line
labelgreen.frame.frame = frame;
To
labelgreen.frame = frame;
declare this function in ur .h
-(void)forview1;
First, try to replace
[UIView setAnimationDidStopSelector:#selector(foeview1)];
with
[UIView setAnimationDidStopSelector:#selector(forview1)];
Second, push label on the top of views using following method
- (void)bringSubviewToFront:(UIView *)view
For example:
UILabel * labelgreen;
[labelgreen.superview bringSubviewToFront:labelgreen];
If you are creating this view in IB then just drag UILabel to the bottom of its subviews.
You should use block based animations. Those functions are deprecated.
[UIView animateWithDuration:animationDuration animations:^{
}];
The label views are nested inside View1/View2/View3 and thus are clipped to the bounds of those containing views. To make them move around the outer view you will need to either start them off in the main view, or before starting the animation remove them from the containing views and add them to the outer view. You could also look into whether the clipToBounds property can be used to have the label views render when they are moved out of the bounds of their containing view.
Example of removing and re-adding in containing view (assuming the labelgreen view is inside a view called view2) (do this before you start your animation).
CGRect newframe = labelgreen.frame
newframe.origin.y = newframe + view2.frame.origin.y;
newframe.origin.x = newframe + view2.frame.origin.x;
[labelgreen removeFromSuperview];
labelgreen.frame = newframe;
Then either:
[colorview addSubview:labelgreen]; // (assuming colorview is your main containing view).
Or:
[self.view addSubview:labelgreen]; // if self.view is the main containing view
I'm trying to implement a UITextView in a table cell at the bottom of a table view.
I've tried the suggestions here Making a UITableView scroll when text field is selected, and other solutions as well, but they're a bit different because I have to artificially add extra height to the current view in order to create space for the keyboard.
Here's what I added to the previous solution in order to port it to my app.
-(void) keyboardWillShow:(NSNotification *)note {
CGRect frame = self.view.frame;
frame.size.height += keyboardHeight;
frame.origin.y -= keyboardHeight;
self.view.frame = frame;
}
-(void) keyboardWillHide:(NSNotification *)note
{
CGRect frame = self.view.frame;
frame.size.height -= keyboardHeight;
frame.origin.y += keyboardHeight;
}
Doing this will correctly add the height to the view and scroll to the cell, but after restoring the original view's height, scrolling beyond the current visible view becomes impossible, even though there is valid content outside of the boundaries (I see the text view before the scroll bar bounces back).
If I try to save the tableview's frame or bounds (not the view) in keyboardWillShow and restore them in keyboardWillHide, the scrolling will be restored, but the view will be cut in half.
Are there any remedies to this besides hard-coding the additional height to the bottom of the view?
I was able to solve my problem of the locked scrolling by removing the code that edits the view's origin. In addition, I implemented scrolling to the bottom cell by using the tableview's contentSize property in my calculations.
-(void) keyboardWillShow:(NSNotification *)note
{
if(!isKeyboardShowing)
{
isKeyboardShowing = YES;
CGRect keyboardBounds;
[[note.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &keyboardBounds];
CGFloat keyboardHeight = keyboardBounds.size.height;
CGRect frame = self.view.frame;
frame.size.height += keyboardHeight;
self.view.frame = frame;
CGPoint scrollPoint = frame.origin;
scrollPoint.y += _tableView.contentSize.height - keyboardHeight;
[_tableView setContentOffset:scrollPoint animated:YES];
}
}