XCode:如何在文本字段上添加左右填充?

时间:2013-09-08 09:50:55

标签: ios objective-c xcode

我有这个.h代码:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *fieldEmail;
@property (strong, nonatomic) IBOutlet UITextField *fieldPassword;
@property (strong, nonatomic) IBOutlet UILabel *titleLogin;

- (IBAction)buttonRegister;
- (IBAction)buttonLogin;
- (IBAction)loginFacebook;
- (IBAction)loginTwitter;

@end

这是我的.m代码:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [_fieldEmail setFont:[UIFont fontWithName:@"ABeeZee-Regular" size:14]];
    [_titleLogin setFont:[UIFont fontWithName:@"Raleway-ExtraLight" size:28]];

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)buttonRegister {
}

- (IBAction)buttonLogin {
}

- (IBAction)loginFacebook {
}

- (IBAction)loginTwitter {
}
@end

现在,我想要做的是在这两个文本字段的左侧和右侧添加填充:fieldEmailfieldPassword,因此它有一些空间,因为我将图像作为背景放在两个文本上字段。

我尝试在- (void)viewDidLoad下的.m文件中添加此代码:

UIView *fieldEmail = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
[UITextField setLeftViewMode:UITextFieldViewModeAlways];
[UITextField setLeftView:fieldEmail];

但是我的XCode给了我这个错误信息:

no known class method for selector 'setLeftViewMode'
no known class method for selector 'setLeftView'
顺便说一下,我正在使用XCode 4.6.3。这个版本有特殊代码在文本字段上添加填充吗?谢谢。

1 个答案:

答案 0 :(得分:4)

leftViewModeleftViewUITextField的属性,不是类方法。您需要将它们分配给实例。

yourTextField.leftViewMode = UITextFieldViewModeAlways;
yourTextField.leftView     = fieldEmail;