Decrease size of UITextView to reveal UIButton - ios

I have a text view in my app that has a done button beside it to resign the keyboard. Basically, rather than having the done button permanently beside the text view, I would like the text view to decrease in width when tapped, and then a done button would appear in the space. Then when the user taps the screen or the done button, the button disappears and the text view increases to it's normal size again.
I have already implemented the code for making the button appear when the user starts typing but I would like to change this so that it appears when they tap the text view and I also need help with adding the animation to shrink the text view.
Thanks.

Implement your textview's delegate method textViewShouldBeginEditing:. Then in that method, do something like this:
button.alpha = 0.0f;
button.hidden = NO;
[UIView animationWithDuration:0.3f animations:^{
NSInteger padding = 5;
CGRect textViewFrame = textView.frame;
textViewFrame.width -= CGRectGetWidth(button.frame) - padding;
// Edit
textView.frame = textViewFrame;
// and just to fade the button in as the text field shrinks
button.alpha = 1.0;
}];
And I would do the opposite when you click the button to return the text field to the correct width

Related

Objective-C : Odd Behavior With UITextField

When a user taps a 'Sign In' button on my app, a view animates into view as such:
- (IBAction)popLoginView:(id)sender {
CGRect frame=self.animationView.frame;
frame.origin.y=0;
[UIView animateWithDuration:0.6 animations:^{
self.animationView.frame=frame;
}];
}
The original y origin was 800, so, off the screen.
However, when a user begins to edit a UITextField on the animationView, animationView disappears. The view's original content shows, with the keyboard present. I can confirm that typing into the keyboard still populates the textfield, even though I can't see it anymore :(
Why would this be happening?
If you are using AutoLayout then your frame change will be discarded when AutoLayout next calculates everything. What you need to do is add a constraint to the y position and then animate the constraint instead of the frame

Moving UIView from One Tab to another

I'm making this type of screen.
I've used 3 UIButton and programmatically gave them border. The little red line is a UIView. I've provided a constraint to UIView Equal Widths as Trending Button.
Now my question that when user taps the next button this UIView should move to the next one. View Controller is 600x600 and next button is at 200x0 so if I change the value of UIView to 200 that will be hard coded and it'll alter according to the screen size. I want it to be perfect and don't know any other way.UPDATE:I'm using AutoLayout so for this purpose I used [self.buttonBottomView setTranslatesAutoresizingMaskIntoConstraints:YES]; and when I run the app the buttons were messed up like in the screenshot. I've applied some constraints on buttons.
You can use NSLayoutConstraint ,change it's LeadingConstaint when you tap on the next button.
Code:
- (void)changeSelectedByTapView:(UIView *)tapView {
_currentSelectedView = tapView;
self.lineImageViewLeadingConstaint.constant = tapView.frameX;
self.lineImageViewWidthConstaint.constant = tapView.width;
[UIView animateWithDuration:0.5f animations:^{
[self.lineImageView.superview layoutIfNeeded];
}];
}
change frame of your UIView.
view.frame = CGRectMake(0+buttonWidth*i, y, width, height);
i is index which is 0 for Trending, 1 for Made by the fans and 2 for your rules.
Also when you change frame for your view, it is shown only on one button at a time.

Move text in UITextField with animation

I need to move text in UITextField (from right to left, and then back) with animation like in Safari App in iOS 7 (see the screenshots below).
Is there any way to do it?
I know that I can set textAlignment, but it won't be animated.
Maybe I should change leftView size in UITextField? But I'm not sure how to calculate size, when text should be aligned on the center. Please, help.~
(source: cs424818.vk.me)
You can achieve this behavior by doing this:
- (void)viewDidLoad
{
// add this to viewDidLoad
// keep the alignment left justified
[self.yourTextField setTextAlignment:NSTextAlignmentLeft];
[self.yourTextField addTarget:self action:#selector(textFieldDidChange)forControlEvents:UIControlEventEditingChanged];
}
-(void)textFieldDidChange
{
// this will cause your textfield to grow and shrink as text is added or removed
// note: this will always grow to the right, the lefthand side will stay anchored
[self.yourTextField sizeToFit];
}
Then when you want to move your UITextField from the center to the left you can simply do:
// 1 second animation
[UIView animateWithDuration:1.0 animations:^{
// move to the left side of some containing view
self.yourTextField.center = CGPointMake(containingView.frame.origin.x + self.yourTextField.frame.size.width/2, self.yourTextField.center.y);
}];
Likewise to move from the lefthand side to the center you can do:
[UIView animateWithDuration:1.0 animations:^{
// move to the center of containing view
self.yourTextField.center = CGPointMake(containingView.frame.size.width/2, self.yourTextField.center.y);
}];
Hope this helps!

UITextField repositions itself when resigning responder

My main controller is a UITextFieldDelegate and animates a UITextField when its editing status changes. In the storyboard the view's original position is in the centre of the screen (vertically and horizontally), when it is being edited I position it to the top of the screen. If I now complete the "Go" action from the keyboard, resign first responder on the text field, the text field pops back to its original position. I have tried retaining the frame of the text field in textFieldShouldEndEditing, but it continues to go back to its original position. How can I keep the view in the new position, even when the keyboard resigns?
I animate the view like this
-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField {
if ([textField isEqual: _toField] && !alreadyAnimated) {
[UIView beginAnimations:#"editFieldAnimation" context:nil];
[UIView setAnimationDuration:0.2f];
// move up to field
CGRect textFieldFrame = [_toField frame];
textFieldFrame.origin.y = 58; // default frame top margin
_toField.frame = textFieldFrame;
[UIView commitAnimations];
alreadyAnimated = YES;
}
return YES;
}

UITextView inside a UIScrollView inside a Popover is not fully visible when keyboard is displayed

I have a UIPopover with a UIScrollViewinside it that contains a UITextView at the bottom. When the keyboard shows, the popover is resized as the text view begins to be edited. I want the code below to ensure the text view is visible:
- (void)textViewDidBeginEditing:(UITextView *)textView {
CGRect visRect = textView.frame;
[self.scrollView scrollRectToVisible:visRect animated:NO];
}
The problem is that the code does not make the entire text view visible. Instead, only the text view up to the bottom of the cursor is shown, as shown below:
How can I show the entire text view/ scroll the scrollview to the bottom? I have tried this:
CGPoint bottomOffset = CGPointMake(0, self.scrollView.contentSize.height - self.scrollView.bounds.size.height);
[self.scrollView setContentOffset:bottomOffset animated:YES];
as explained in this answer but nothing works.
In addition, my scrollview is scrolled to the position shown AFTER the keyboard is moved into place. Ideally I'd like the scrolling to happen before or during the keyboard movement.
Any help would be great.
#hey Dave
This By default Case in of UIPopOverController, whenever we used UIPopOverControler to display any PopUpView and in suppose that PopUpView height as large as can be covered by KeyBoard then in that case that PopOverView Automatically gets Shrink itself.as you resign keyboard that PopUpView will expand itself automatically.I have faced same case.
This is just my opinion , you can change the origin of CurrentView(parentView of PopUpView) as keyboard going to display/hide so that PopUpView could display itself properly from and could get the appropriate space.
See Below Are the Delegate methods of UITextView Responds Back as editing start and end.
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
//chnage the OriginY of PopUpView's SUperView
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
//re Adjust the OriginY of PopUpView's SUperView
}
I hope it may helpful to you.
I found the solution:
- (void)keyboardDidShow:(NSNotification *)notification {
NSLog(#"Notification: %s", __PRETTY_FUNCTION__ );
//
CGFloat textviewBottom = CGRectGetMaxY(self.commentsTextView.frame);
CGRect belowTextViewRect = CGRectMake(0, textviewBottom, 350.f, self.scrollView.contentSize.height - textviewBottom);
// NB! This works ONLY: 1) keyboardDidShow 2) Non-animated;
// Does NOT work: 1) animated, 2) keyboardWillShow, 3) textViewDidBeginEditing
[self.scrollView scrollRectToVisible:belowTextViewRect animated:NO];
}

Resources