代码:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. 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; }