If the input box is overwritten by the keyboard and the keyboard is retained

Source: Internet
Author: User

If the input box is overwritten by the keyboard and the keyboard is retained

 

----------- Content in ViewController. m ------------

# Import "ViewController. h"

# Import "ScreenView. h"

@ Interface ViewController () <UITextFieldDelegate>

{

ScreenView * _ screenV; // overwrite the full screen

UIView * secView;

}

@ End

 

@ Implementation ViewController

 

-(Void) viewDidLoad {

[Super viewDidLoad];

[Self textFieldOnView: self. view]; // Add a subview

[Self coverBtnOnView: self. view]; // Add button

}

-(Void) textFieldOnView :( UIView *) currentV {

UITextField * tF1 = [[UITextField alloc] init];

TF1.frame = CGRectMake (30,100,100, 40 );

TF1.delegate = self;

# Pragma ------ set tF1.tag = 1;

TF1.tag = 1;

TF1.borderStyle = UITextBorderStyleRoundedRect;

[CurrentV addSubview: tF1];

UITextField * tF2 = [[UITextField alloc] init];

TF2.frame = CGRectMake (30,500,100, 40 );

TF2.delegate = self;

TF2.borderStyle = UITextBorderStyleRoundedRect;

[CurrentV addSubview: tF2];

SecView = [[UIView alloc] initWithFrame: CGRectMake (150,200,200,300)];

SecView. backgroundColor = [UIColor yellowColor];

[CurrentV addSubview: secView];

UITextField * textF1 = [[UITextField alloc] init];

TextF1.frame = CGRectMake (30,100,100, 40 );

TextF1.delegate = self;

TextF1.borderStyle = UITextBorderStyleRoundedRect;

[SecView addSubview: textF1];

UITextField * textF2 = [[UITextField alloc] init];

TextF2.frame = CGRectMake (30,250,100, 40 );

TextF2.delegate = self;

TextF2.borderStyle = UITextBorderStyleRoundedRect;

[SecView addSubview: textF2];

}

 

-(Void) coverBtnOnView :( UIView *) currentV {

UIButton * coverBtn = [UIButton buttonWithType: UIButtonTypeSystem];

CoverBtn. backgroundColor = [UIColor cyanColor];

If (! _ ScreenV ){

[CoverBtn setTitle: @ "cover full screen" forState: UIControlStateNormal];

[CoverBtn addTarget: self action: @ selector (coverScreen) forControlEvents: UIControlEventTouchUpInside];

} Else {

[CoverBtn setTitle: @ "return" forState: UIControlStateNormal];

[CoverBtn addTarget: self action: @ selector (removeCoverScreen) forControlEvents: UIControlEventTouchUpInside];

}

UIView * v = [self. view viewWithTag: 1];

CoverBtn. frame = v. frame;

CGRect temp = coverBtn. frame;

Temp. origin. x + = temp. size. width;

CoverBtn. frame = temp;

[CurrentV addSubview: coverBtn];

}

 

-(Void) removeCoverScreen {

[_ ScreenV removeFromSuperview];

}

 

-(Void) coverScreen {

_ ScreenV = [[ScreenView alloc] initWithFrame: [UIScreen mainScreen]. bounds];

# Pragma ----- view transparency

_ ScreenV. backgroundColor = [[UIColor brownColor] colorWithAlphaComponent: 0.5];

# Pragma ----- view and its subview transparency

// ScreenV. alpha = 0.7;

# Pragma ----- create a view to overwrite the full screen. You need to create a ScreenView class and override the touchesBegan method.

[[UIApplication sharedApplication]. keyWindow addSubview: _ screenV];

# Pragma ----- if self. view is full screen size, the following sentence is feasible. _ screenV is initialized directly with UIView and can respond to touch events.

// [Self. view addSubview: _ screenV];

[Self textFieldOnView: _ screenV]; // Add textField

[Self coverBtnOnView: _ screenV];

}

 

// When you start to edit the input box, the keyboard appears. Execute this event.

-(Void) textFieldDidBeginEditing :( UITextField *) textField

{

UIView * tempView = self. view;

If (_ screenV ){

TempView = _ screenV;

}

UIWindow * window = [[UIApplication sharedApplication] delegate] window];

CGRect rect = [textField convertRect: textField. bounds toView: window]; // convert the coordinates of textField to those of its parent view.

NSInteger y = rect. origin. y + textField. frame. size. height;

NSInteger offset = (y-(tempView. frame. size. height-256.0); // The keyboard height is 216. // how do I obtain the keyboard height?

NSTimeInterval animationDuration = 0.30f;

[UIView beginAnimations: @ "ResizeForKeyboard" context: nil];

[UIView setAnimationDuration: animationDuration];

// Move the Y coordinate of the view to the offset Unit so that the following space is used for the display of the soft keyboard.

If (offset> 0)

TempView. frame = CGRectMake (0.0f,-offset, tempView. frame. size. width, tempView. frame. size. height );

[UIView commitAnimations];

}

 

/// When the user presses the return key or the return key, the keyboard disappears.

//-(BOOL) textFieldShouldReturn :( UITextField *) textField

//{

// [TextField resignFirstResponder];

// Return YES;

//}

 

-(Void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {

[Super touchesBegan: touches withEvent: event];

[Self. view endEditing: YES];

}

 

// After the input box is edited, the view is restored to the original state.

-(Void) textFieldDidEndEditing :( UITextField *) textField

{

UIView * tempView = self. view;

If (_ screenV ){

TempView = _ screenV;

}

TempView. frame = CGRectMake (0, 0, tempView. frame. size. width, tempView. frame. size. height );

}

-(Void) didReceiveMemoryWarning {

[Super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

 

----------------- Content in ScreenView. m -----------------

# Import "ScreenView. h"

 

@ Implementation ScreenView

 

-(Void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {

[Super touchesBegan: touches withEvent: event];

[Self endEditing: YES];

}

@ End

 

Please correct us for any omissions.

END

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.