I am adding imageview in viewcontroller.
It is working good in ios6 but in ios7 It display with white background.
I have tried clearColor but it's not working.
Here is my code
[imgcharacter setImage:[UIImage imageNamed:[NSString stringWithFormat:#"character%d.png",reelBtnTag]]];
[imgcharacter setFrame:CGRectMake(100,100, 247.5f, 385.0f)];
imgcharacter.userInteractionEnabled=YES;
[imgcharacter setBackground : [UIColor clearColor]];
[captureview addSubview:imgcharacter];
Try this way....
NSData *imageData = UIImagePNGRepresentation(Your Image);
UIImage *image=[UIImage imageWithData:imageData];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 52, 40, 50)];
imageView.backgroundColor = [UIColor clearColor];
imageView.image = image;
Related
I have created a button using code shown below -
UIImage *kalenderImage = [UIImage imageNamed:#"start_icon_calendar_u.png"];
UIImageView *kalenderImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kalenderImage.size.width/2, kalenderImage.size.height/2)] autorelease];
[kalenderImageView setImage:kalenderImage];
UILabel* kalendarLabel = [[[UILabel alloc] initWithFrame:CGRectMake (0, kalenderImage.size.height/2-15, kalenderImage.size.width/2, kalenderImage.size.height/2)] autorelease];
kalendarLabel.text = #"Kalender";
[kalenderButton addSubview:kalenderImageView];
[kalenderButton addSubview:kalendarLabel];
[kalenderButton addTarget:self action:#selector(showCalendar:) forControlEvents:UIControlEventTouchUpInside];
I need to change the image of button for UIControlStateHighlighted state. How can i do this ?
I dont want to use
KalenderButton setBackgroundImage:#"" forState:]
[KalenderButton setImage:#"" forState]
Its very simple. Look at the following code. It checks the file is locked and changes the image of the button accordingly. You can edit the code according to your necessity.
if (fileLocked) {
UIImage *image = [UIImage imageNamed: [NSString stringWithFormat:#"icon-lock.png"]];
[lockButton setImage:image forState:UIControlStateNormal];
} else {
UIImage *image = [UIImage imageNamed: [NSString stringWithFormat:#"icon-unlock.png"]];
[lockButton setImage:image forState:UIControlStateNormal];
}
It works.
I am trying to combine two image views and put them into another image view which I am assigning to background image of a button.Somehow this is not working.Could someone please check and let me know what exactly is wrong here.
UIImageView * image1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"homesmall.png"]];
UIImageView * image2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"update.png"]];
[image1 setFrame:CGRectMake(16,0,16,16)];
[image2 setFrame:CGRectMake(0,0,16,16)];
UIImageView *cellAcc = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,32,16)];
[cellAcc addSubview:image1];
[cellAcc addSubview:image2];
UIImage *image = [cellAcc image];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
button.frame = frame;
[button setBackgroundImage:image forState:UIControlStateNormal];
You are adding them as subviews. There is no additive property here. You will have to use quartz to draw them into an image context and extract the combined image.
Edit
You can replace
UIImage *image = [cellAcc image];
with
UIGraphicsBeginImageContext(cellAcc.bounds.size);
[cellAcc.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
I need so set the name of the UIViewController and an image to the navigationBar. So far I am able to display the image, but the title is of course missing.
// show image
UIImage *image = [UIImage imageNamed: #"bar_icon.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
imageView.frame = CGRectMake(0, 0, 40, 40);
self.navigationItem.titleView = imageView;
[imageView release];
Thanks for your help,
BR, doonot
// show image
UIImage *image = [UIImage imageNamed: #"bar_icon.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
imageView.frame = CGRectMake(70, 0, 40, 40);
// label
UILabel *tmpTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 0, 100, 40)];
tmpTitleLabel.text = #"Mannschaft";
tmpTitleLabel.backgroundColor = [UIColor clearColor];
tmpTitleLabel.textColor = [UIColor whiteColor];
CGRect applicationFrame = CGRectMake(0, 0, 300, 40);
UIView * newView = [[[UIView alloc] initWithFrame:applicationFrame] autorelease];
[newView addSubview:imageView];
[newView addSubview:tmpTitleLabel];
self.navigationItem.titleView = newView;
[imageView release];
[tmpTitleLabel release];
Create a UIView with 2 subview: a UIImageView and a UILabel. Set the titleView to the UIView.
I'm doing this to load an image into a tableview cell:
UIImage *image = [UIImage imageNamed:#"myimage.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake( 0, 0, cell.frame.size.width, cell.frame.size.height );
imageView.contentMode = UIViewContentModeScaleToFill;
[cell addSubview:imageView];
[imageView release];
return cell;
However, no image displays as the cell's background. This is in a new navigation app. The above looks just like the steps in Interface Builder when you are using a custom cell. The image is in the app bundle. What am I doing wrong?
Are you trying to set the cell's background, or just add an image? If you're trying to set its background, you should use the cell.background property.
imageView.frame = cell.bounds;
imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
cell.backgroundView = imageView;
How can I set up a background image to UITextView?
You can have an UIImageView containing the background image and the UITextView as siblings, then in Interface Builder move the text view to overlap the image view (or add them both to the same parent view if doing it programmatically). You also need to make sure that text view is not opaque and give it a 0% opacity background.
UITextView *textView = [[UITextView alloc]initWithFrame: window.frame];
textView.text = #"text\n text\n text";
UIImageView *imgView = [[UIImageView alloc]initWithFrame: textView.frame];
imgView.image = [UIImage imageNamed: #"myImage.jpg"];
[textView addSubview: imgView];
[textView sendSubviewToBack: imgView];
[window addSubview: textView];
#durai: Your image has a height limit after which white background will appear if empty background appears after it scrolls down then you can repeat the same image.
This might be helpful:
textView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed: #"Image.png"]];
Just one clarification, when trying out answer #9 from #oxigen, I found out that this line:
UIImageView *imgView = [[UIImageView alloc]initWithFrame: textView.frame];
Is relative to the textView.frame. So your x and y values need to be 0,0 if you want it to overlap completely which means you want something like:
UIImageView *imgView = [[UIImageView alloc]initWithFrame:textView.bounds];
I found one simple method how to set background image.
h file
#interface FNTextView : UITextView
#end
m file
...
- (void)drawRect:(CGRect)rect
{
[self.bgImage drawInRect:rect];
[super drawRect:rect];
}
- (void)initHandler
{
self.bgImage = [[UIImage imageNamed:#"textview_bg"] resizableImageWithCapInsets:UIEdgeInsetsMake(4, 4, 4, 4)];
}
...
[txtView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"image.png"]]];
For tiled background i like this solution
UIImage *patternImage = [UIImage imageNamed:#"image.png"];
UIColor* color = [UIColor colorWithPatternImage:patternImage];
textView.backgroundColor = color;
Not sure about, but you can try the following, assuming that your background image is handled by a UIImageView:
[myTextView addSubview: myImageView];
Note that you may need to change the value of the alpha/opaque properties of your UITextView.
Kind Regards.
UITextView *textView=[[UITextView alloc]initWithFrame: CGRectMake(20, 20, 40, 40)];
textView.text = #"this is a test \n this is test \n this is a test";
UIImageView *img = [[UIImageView alloc]initWithFrame: textView.frame];
img.image = [UIImage imageNamed: #"image.png"];
[self.view addSubview:textView];
[self.view insertSubview:img belowSubview:textView];
The answers of #dulcanwilcox and #oxigen both work, but, an addition would be to make the background image resizable, in case the image is being used to show some kind of border.
UITextView *textView = [[UITextView alloc]initWithFrame: window.frame];
textView.text = #"Some text...";
UIImageView *imgView = [[UIImageView alloc]initWithFrame: textView.frame];
imgView.image = [[UIImage imageNamed: #"image"] resizableImageWithCapInsets:UIEdgeInsetsMake(8, 8, 8, 8)];
[textView addSubview:imgView];
[textView sendSubviewToBack:imgView];
[window addSubview:textView];
Check out the documentation for resizableImageWithCapInsets:(UIEdgeInsets)capInsets.