【代碼筆記】改變文字輸入框背景,代碼筆記文字輸入框
一,。
二,工程圖。
三,代碼。
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;}