3

First sorry for my poor English!!

In my app have a UIViewController and UIView is called subViewLogin inside subViewLogin have 2 UITextField are called username and password.When my app run subViewLogin is moved to position by UIViewAnimationCurveLinear.But when I finish editting UITextField (resignFirstResponder is called) subViewLogin comeback to position.Why? And how can I prevent subViewLogin from it comeback to position?

Here are my code.

//LoginViewController.h
#import <UIKit/UIKit.h>

@interface LoginViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *username;
@property (weak, nonatomic) IBOutlet UITextField *password;
@property (weak, nonatomic) IBOutlet UIImageView *background;
@property (weak, nonatomic) IBOutlet UIImageView *logoEnergy;
@property (weak, nonatomic) IBOutlet UIImageView *logoKmutl;
@property (weak, nonatomic) IBOutlet UIImageView *logoProjectname;
@property (weak, nonatomic) IBOutlet UIView *subviewLogin;

- (void)advoidSubviewFromKeyboard;
- (IBAction)usernameAndPasswordBeginEdit;
- (IBAction)dismissKeyboard;
@end

and

//LoginViewController.m
#import "LoginViewController.h"
@implementation LoginViewController

const float keyBoardPortraitHeight = 216;
const float keyBoardLandscapeHeight = 162;

@synthesize username, password, background, logoEnergy, logoKmutl, logoProjectname;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];
    UIView *subview = self.subviewLogin;
    subview.frame = CGRectMake(subview.frame.origin.x, self.view.frame.size.height, subview.frame.size.width, subview.frame.size.height);
}

- (void)viewDidAppear:(BOOL)animated
{
    [UIView
    animateWithDuration:0.3
    delay: 0.0
    options: UIViewAnimationCurveLinear
    animations:^{
        UIView *subview = self.subviewLogin;
        subview.frame = CGRectMake(subview.frame.origin.x, self.view.frame.size.height-subview.frame.size.height-30, subview.frame.size.width, subview.frame.size.height);
    }
    completion:^(BOOL finished){

    }
    ];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)advoidSubviewFromKeyboard
{
    [UIView
     animateWithDuration:0.3
     delay: 0.0
     options: UIViewAnimationCurveLinear
     animations:^{
         self.view.frame = CGRectMake(self.view.frame.origin.x, -keyBoardPortraitHeight+30, self.view.frame.size.width, self.view.frame.size.height);
     }
     completion:^(BOOL finished){

     }
     ];
}

- (IBAction)usernameAndPasswordBeginEdit
{
    [self advoidSubviewFromKeyboard];
}

- (IBAction)dismissKeyboard
{
    NSLog(@"dismiss");
    //[self.username becomeFirstResponder];
    //[self.username resignFirstResponder];
    [self.subviewLogin endEditing:YES];
    //[self.view endEditing:YES];
}
@end

1 Answers1

0

Even though the question was asked a long time ago, that behavior still presents nowadays. I've solved it using constraints instead of updating frame of the element. Example:

func expand(with view: UIView, constraint: NSLayoutConstraint) {
    constraint.constant = self.isExpanded ? self.minHeight : self.maxHeight //set needed constant here
    UIView.animate(withDuration: expandDuration, animations: {
        self.arrowImageView.transform = self.isExpanded ? .identity : CGAffineTransform(rotationAngle: self.topAngle)
        view.layoutSubviews() //layout constraint's view subviews
    }) { _ in
        self.isExpanded = !self.isExpanded
    }
}
coldembrace
  • 549
  • 8
  • 19