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

一,效果图。

二,工程图。

三,代码。

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;
}
复制代码

 

 

 
 
原文地址:https://www.cnblogs.com/yang-guang-girl/p/5240936.html