Reputation: 251
Although I have set the background image property in UITextField
. It doesn't apply.I didn't use any coding. I have just selected an image.png
file as the backround image using Interface Builder. So,How to do it ?
And there is no background image property in UITextView
. So,How can I add background image to textView
?
Thanks for the help.
Upvotes: 4
Views: 25551
Reputation: 12034
Swift 4
First one, add the image to your Assets.xcassets
with the name you want, like myImage
. In your textfield, you can set it with:
myTextField.background = UIImage(named: "myImage")
Also, if you want to remove the borders:
myTextField.layer.borderColor = CGColor.clear
myTextField.layer.borderWidth = 0
Upvotes: 2
Reputation: 280
CGRect frame1 = CGRectMake(30, 130,160, 30);
textField = [[UITextField alloc] initWithFrame:frame1];
textField.borderStyle =UITextBorderStyleNone;
textField.background=[UIImage imageNamed:@"textFieldImage.png"];
Upvotes: 0
Reputation: 31081
You can set TextView backGround image using
[textView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImage.png"]]];
And for yourTextField you can set image using 2 method first is previous and other is
[textField setBackground:[UIImage imageNamed:@"yourImage.png"]];
Upvotes: 1
Reputation: 1735
Please refer to the documentation of UITextField and you will find the following:
The default value for this property is UITextBorderStyleNone. If the value is set to the UITextBorderStyleRoundedRect style, the custom background image associated with the text field is ignored.
In Interface Builder, just below the place where you set the background image, there should be an option to select a border style, by default it is selected to RoundedRect, select another style and you can immediately see the background image.
Upvotes: 21
Reputation: 107
Use this
textField.backgroundColor = [UIColor colorWithPatternImage:myImage];
where "myImage" is your image.
Upvotes: 6
Reputation: 9687
To add a background image to views that have no background image property, you will need to do some trickery. You will need to create a UIImageView and then add the UITextView as a subview. Make sure the UITextView background color is set to clear as well. For example:
UIImageView *imageView = [[UIImageView alloc] initWithimage:myImage];
[self.view addSubview:imageView];
[imageView addSubview:myTextView];
Upvotes: -2