I'm trying to add a back ground image to UITextView. The image is just a small border which will be placed at the top of text view. (the image resembles teared paper image). I'm using following code
UIImageView *imgView = [[UIImageView alloc]initWithFrame: CGRectMake(0, 0, 320, 13)];
imgView.image = [UIImage imageNamed: #"teared_paper.png"];
[tView addSubview: imgView];
[tView sendSubviewToBack: imgView];
[imgView release];
My text view's height is 150 pixels only. (text view occupies only small portion of view and it is at the top of the view so that it will appear to the user when keyboard is there)
The problem is that, when I add more lines of text, text view is scrolling automatically. And at the same time, the background image that I added is also scrolling. How can I prevent the background image to stay on the top all the time irrespective of scrolling.
Don't add the image view as a subview of the text view. Instead, have both the image view and the text view as children of the main view, position the image view behind the text view, and set the background color of the text view to transparent with:
[tView setBackgroundColor:[UIColor clearColor]];
Can you try putting a UIImageView behind the UITextView and making the UITextView transparent?
Try it with:
UIImageView *imgView = [[UIImageView alloc]initWithFrame: CGRectMake(0, 0, 320, 13)];
imgView.image = [UIImage imageNamed: #"teared_paper.png"];
[tView addSubview: imgView];
[imgView release];
Just make textView transparent :
[textView setBackgroundColor:[UIColor clearColor]];
no need to bring the subview to front.
Hope this works for u..
Related
After adding a background (image) to my UIView (View_0) I wish to implement a button to remove the same background.
How can I do it? I have tried to set a different image as the background of View_0 but this does not work (I suspect this background is set behind "image").
I don't want to use
[View_0 removeFromSuperview];
as I need View_0 to still be there for other purposes....
//Create an ImageView
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 940, 422)];;
//Add the ImageView as the background of the View_0
[View_0 addSubview:image];
//Move custom image behind View
[View_0 sendSubviewToBack: image];
//Failed attempt to remove the background....
View_0.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"defaultImage.PNG"]];
You have added a UIImageView to the View and Not the background of the view. So what is happening is that when u implement
View_0.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"defaultImage.PNG"]];
The background of the View is being set but the UIImageView is now on top of the View so that is why the background of the View is not being shown.
Instead if you simply want to add the background you can change it to the Action of the UIButton.
Also if you use [View_0 removeFromSuperview]; you are trying to remove the view which is not right.
//Create an ImageView
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 940, 422)];;
image.tag = 1234;
//Add the ImageView as the background of the View_0
[View_0 addSubview:image];
//Move custom image behind View
[View_0 sendSubviewToBack: image];
UIImageView *imgView = [self.view viewWithTag:1234];
[img setImage:nil];
This should work i think
Is it possible to add an image to the nav bar? The image should be viewed only in a certain view (i.e. not throughout the application)... Example with an earth icon:
http://i.stack.imgur.com/FsyH8.png
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
imgView.image = [UIImage imageNamed: #"1.bmp"];
[self.navigationController.navigationBar addSubview:imgView];
[self.navigationController.navigationBar bringSubviewToFront:imgView];
[imgView release];
Yup! Same way you would add any other subView to a view.
[myNavBar addSubView: myIconView];
You can also add the image in interface builder if you don't want to do it programmatically. Select a button in interface builder and there should be an option to use an image for that item in the Attributes Inspector under "Bar Item"
I have an UITableview controller without an XIB.
I set the background colour of the tableview by doing this:
[self.tableView setBackgroundColor:[UIColor colorWithPatternImage:
[UIImage imageNamed:#"MmyImage.png"]]];
My image is a gradient image, so eventhough the above code is ok, it redraws the image for each cell when the tableview goes to edit view, and it appears as lines which is not looking elegant.
I would like to set the tableviews background to clear colour and set the superview's colour to the image, so that the tableview transitions smoothly over the superview. However the below code does not work:
[self.view setBackgroundColor:[UIColor colorWithPatternImage:
[UIImage imageNamed:#"myImage.png"]]];
[self.tableView setBackgroundColor:[UIColor clearColor]];
This makes the background completely white, I dont know why.
Help appreciated..thanks.
Edited:new code but this does not work as well:
UIImageView * imgBg = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:#"myImage.png"] stretchableImageWithLeftCapWidth:6 topCapHeight:6]];
imgBg.frame = self.tableView.window.bounds;
[self.tableView.superview addSubview:imgBg];
//[self.view addSubview:imgBg];
//[self.tableView.window sendSubviewToBack:imgBg];
//[self.view bringSubviewToFront:self.tableView];
[self.tableView setBackgroundColor:[UIColor clearColor]];
[imgBg release];
In a custom UITableViewController subclass, self.view will return the same as self.tableView. So what you're doing there is altering the same object. In the end, you get to see the UIWindow. So to get the desired result, you should do this –
[self.view.window setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"myImage.png"]]];
[self.tableView setBackgroundColor:[UIColor clearColor]];
UITableView has a backgroundView property. Use it.
aTableView.backgroundView = [[[UIView alloc] initWithFrame:aTableView.bounds] autorelease];
aTableView.backgroundView.backgroundColor = backgroundColor;
First in your xib file, add a parent view. add table view in that parent view. set parent view's background color to your desired background. then set your tableview's background color to [UIColor clearColor].
Check also, if your table view is a group table style or not? if it is group table style, then the cell have distinct background. Then you need to clear the background of each cell. The best way to do is,
create a blank UIView *bkview = [[UIView alloc] initWithRect:CGRect(0,0)];
set this
as your cells background view.
I hope this will work.
then your second code will be right, except:
[self.view setBackgroundColor:[UIColor colorWithPatternImage:
[UIImage imageNamed:#"myImage.png"]]];
replace this:
UIImageView * imgBg = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:#"myImage.png"] stretchableImageWithLeftCapWidth:6 topCapHeight:6]];
imgBg.frame = self.view.bounds;
[self.view addSubview:imgBg];
[self.view sendSubviewToBack:imgBg];
Edit
I think:
[self.tableView setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0]]; will helps, that is the only difference now.
I am subclassing a UITableViewCell and in the init function I am doing the following:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"packagelistcell_background.png"]];
[imageView setFrame:CGRectMake(0, 0, 300, 81)];
self.backgroundView = imageView;
[imageView release];
When that displays on the screen, the image appears slightly blurred. For testing, I placed a straight UIImageView above the table view and the image looks perfect and crisp. The table view is a grouped table view. Do you know why it is blurring the image?
I think the cell has been resized and so is the backgroundView.
Try
imageView.autoresizingMask = UIViewAutoresizingNone;
Try shifting the imageView by 0.5 pixels.
Unfortunately #freytag's solution didn't work for me.
To solve this problem I had to add the UIImageView to a UIView container and set this last as background view of my Custom Cell instead of setting directly the UIImageView:
UIImageView *imgView = [[UIImageView alloc] initWithImage:#"cellBackground.png"];
UIView *backgrContainer = [[UIView alloc] initWithFrame:self.backgroundView.frame];
[backgrContainer addSubview:imgView];
[self setBackgroundView:backgrContainer];// instead of [self setBackgroundView:imgView];
[backgrContainer release];
[imgView release];
For some reason setting directly the UIImageView makes it blurry (even if no resize ocurrs). Setting autoresizingMask = None or contentMode <> ScaleToFill for the UIImageView did not fix my problem.
Also beware of the UITableView style, since using a Grouped Style will result in less width for the cell's background view, and this can also resize the image.
( Retina device/simulator doesn't seem to have this problems about blurry image in the cell backgroundVIew)
i want to get a multiple line string from a UITextView and draw it on a image,
the position of text in the image must exactly same as the position in UITextView but i dont want jus add the textView to UIImageView, i need a new image consists of those text, is it possible? any suggestion to do that?
You can capture the contents of any UIView into a UIImage. First, create an empty UIView and position your UIImageView and UITextView as subviews:
// Assumes you have UIImageView *myImageView and UITextField *myTextField
UIView *parentView = [[UIView alloc] initWithFrame:CGRectZero];
[parentView addSubview:myImageView];
[myImageView setFrame:CGRectMake(0, 0, 100, 100)];
[parentView addSubview:myTextField];
[myTextField setFrame:CGRectMake(20, 20, 80, 20)];
[parentView sizeToFit];
Now create a graphics context and draw parentView into it:
UIGraphicsBeginImageContext([parentView bounds].size);
[[parentView layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
You now have a UIImage with the contents of your UIImageView and UITextField to display, save, or send over the network.
This was a good solution, thanks.
I would like to add that you need to add
#import <QuartzCore/QuartzCore.h>
To your imports so the warnings go away.