一,效果图。
二,工程图。
三,代码。
RootViewController.h
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController
<UITextFieldDelegate>
@end
RootViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//初始化背景
[self addView];
}
#pragma -mark -functions
-(void)addView
{
UITextField *textField=[[UITextField alloc]initWithFrame:CGRectMake(50, 100, 200, 50)];
textField.backgroundColor=[UIColor redColor];
textField.delegate=self;
[self.view addSubview:textField];
}
#pragma -mark -UITextFieldDelegate
-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField{
[textField setBackground:[UIImage imageNamed:@"1.jpg"]];
return YES;
}
-(void) textFieldDidEndEditing:(UITextField *)textField{
[textField setBackground:[UIImage imageNamed:@"2.jpg"]];
}
-(BOOL)textFieldShouldReturn: (UITextField *)TextField{
[TextField resignFirstResponder];
return YES;
}