您的位置:首页 > 编程语言

【代码笔记】改变文字输入框背景

2016-03-04 09:34 239 查看
一,效果图。



二,工程图。



三,代码。

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;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: