将图像放在UITextfield的底部作为背景

时间:2012-04-26 13:16:08

标签: iphone ios xcode ios5 uitextfield

经过大量搜索后,我发布了这个问题,我必须在UITextfield的底部放置一个图像,我已经尝试过以下代码:

letterField.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
letterField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
letterField.textAlignment = UITextAlignmentCenter;
letterField.borderStyle = UITextBorderStyleNone;
UIImageView *myView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"text_line.png"]];
[letterField  setLeftView:myView];
[letterField  setRightViewMode:UITextFieldViewModeAlways];
[myView release];
// letterField.background = [UIImage imageNamed:@"text_line.png"];  
// [letterField setBackground:[UIImage imageNamed:@"text_line.png"]];  
letterField.textColor = [UIColor whiteColor];  
letterField.returnKeyType = UIReturnKeyDone;

2 个答案:

答案 0 :(得分:0)

[yourtextfield insertSubview:imgViewTitleBackGround atIndex:0];// mgViewTitleBackGround is image view with background image.

//或

[yourtextfield setBackground:[UIImage imageNamed:@"text_line.png"]];// you can add your image
yourtextfield.textColor = [UIColor redColor];

希望,这会帮助你......享受

答案 1 :(得分:0)

对不起,我之前的回答是完全错误的。从我可以看到你的图像是text_line.png - 我想它是某种分隔线,我建议你尝试创建一个带有UITextField边界的新UIView,然后在该视图中添加文本字段并添加UIImage视图再次作为该视图的子视图正确居中。我不知道这是否符合您的需求,但您可以尝试一下。