Send UIImageView (label background) to back to display UILabel - uiimageview

I am trying to add an image as a background to a UILabel, but my UILabel's title cannot be seen, even though I tried to send the background image to the back. My code is below and any advice on how to help with this would be great, thanks!
UIImageView *labelBackground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image.png"]];
[myLabel addSubview:labelBackground];
[myLabel sendSubviewToBack:labelBackground];
myLabel.backgroundColor = [UIColor clearColor];
[myLabel setText:title];

When you add a view (your image view) as a subview to another view (your label), the subview will always be in front of its superview. They would either need to be siblings:
[myContainer addSubview:labelBackground];
[myContainer addSubview:myLabel];
or better yet, the label should be a subview of the image view:
[labelBackground addSubview:myLabel];
[myContainer addSubView:labelBackground];
Another solution might be to use your image as a background color for your label:
[myLabel setBackgroundColor:[UIColor colorWithPatternImage:myUIImage]];
But note that the image will be repeated instead of centered or stretched.

Try adding the UILabel as a subview of the UIImageView, rather than the other way around.
This will result in the UIImageView being the "container" of the label, and thus the label being on top of the UIImageView.

Related

How to make TabBar transparent

I want to make the tabbar transparent and leave the icons still there. So that when you look at it the icons on the tabbar look like they are their by themselves. Whats the code for me to do this? Right now this is the code i have
UIImage* tabBarBackground = [UIImage imageNamed:#""];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:#""]];
Try this code
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame = CGRectMake(0.0, 0, self.view.bounds.size.width, 48);
UIView *trans_view = [[UIView alloc] initWithFrame:frame];
[trans_view setBackgroundColor:[[UIColor alloc] initWithRed:0.0
green:0.0
blue:0.0
alpha:0.5]];//you can change alpha value also
[tabBar1 insertSubview:trans_view atIndex:0];//tabBar1 = your tabbar reference
[trans_view release];
}
this link also will help you
The easiest way to make a tab bar transparent is by setting the tab bar background image to a transparent image in the interface builder.
You can get a transparent png image whose height and width is equal to the tab bar's from the net.
Note: By changing the alpha value, you actually end up dimming the tab bar's icons as well. Make sure this is what you want, otherwise using a transparent background image is a better option.

How do i make a simple scrollview height = 80, width = 280, with images inside thats scrolls horizontally?

I need to make a simple scroll view in xcode with width of 280 and height of 80 and with images inside thats scrolls horizontally. i want to make this programmatically.
I assume you mean the UIScrollview, which has a guide written by apple found here:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIScrollView_Class/Reference/UIScrollView.html
A guide that I personally used was this one:
http://idevzilla.com/2010/09/16/uiscrollview-a-really-simple-tutorial/
I'll take you through the quick basics of adding the scrollview to your view and adding images to it.
I'm guessing you're new to Objective C, so I'll give you a quick guide. Firstly, you'll want to make a UIScrollView object. This is done by declaring the following:
UIScrollView *aScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake (0,0,320,250)];
You'll notice I set the frame. The first two numbers of CGRectMake give you the x and y origin of the point while the last two numbers are for how wide and tall you want your object to be.
Afterwards, you'll want to add images to it. You'll need a UIImageview.
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 250)];
Note that I positioned the image at 0,0, giving it a height of a 250 and a width of 320. This ensures that it fills entire scrollview's initial view.
imageView.image = [UIImage imageNamed:#"foo.png"];
You'll attach an image to the imageView. But wait, there's more. So far you've created these objects but have not yet associated them with the view. So if we are in a ViewController class (you'll have to look up what that is), the ViewController contains a view. We can attach our objects to the view.
[aScrollView addSubview:imageView]; // Adds the image to the scrollview
[self.view addSubview:aScrollView]; // Adds the scrollview to the view.
If you want to add more images, you have to add them at different x origins. So our first added image was at 0,0. Our next added image should be at 320,0 (because the first image took up 320 pixels width).
UIImageView *secondImageView = [[UIImageView alloc] initWithFrame:CGRectMake(320, 0, 320, 250)];
secondImageView.image = [UIImage imageNamed:#"bar.png"];
[aScrollView addSubview:secondImageView];
There are a number of options for scrollview that you will want to explore. The ones I found useful were:
aScrollView.delegate = self; // For gesture callbacks
self.pagingEnabled = TRUE; // For one-at-a-time flick scrolling
self.showsHorizontalScrollIndicator = NO; // Cleaner look for some apps.
self.alwaysBounceHorizontal = TRUE; // Look it up.

Draw NSView background partially, with a gradient

I have a NSView, subclassed, with custom background drawing, filling it with a gradient.
In IB, I've put a checkbox on it, somewhere in the middle.
This is the drawRect method.
-(void) drawRect:(NSRect)dirtyRect {
CGFloat sc = 0.9f;
CGFloat ec = 0.6f;
NSColor* startingColor = [NSColor colorWithDeviceRed:sc green:sc blue:sc alpha:1];
NSColor* endingColor = [NSColor colorWithDeviceRed:ec green:ec blue:ec alpha:1];
NSGradient *grad = [[NSGradient alloc] initWithStartingColor:startingColor endingColor:endingColor];
[grad drawInRect:dirtyRect angle:270];
}
What happens is, this same method gets called to draw the whole view area first and then for the part, where NSButton (checkbox) lies on top of it. OF course the checkbox background is drawn with a complete gradient and it is not right, since the portion is much smaller. The same happens with other controls I put on the said NSView.
What is the suggested approach on such thing?
One option is to make controls height the same as the views' but this will result in problems in the future.
The answer is, always draw the WHOLE area of the view, not just the dirtyRect
[grad drawInRect:[self bounds] angle:270];

Place a background Image behind a grouped UITableView

I've a grouped UITableView which has been resized and rounded. I would like to place a view BEHIND this table.I've tried:
[self.tableView addSubview:backgroundView];
[self.tableView sendSubviewToBack:backgroundView];
but it didn't work. This is what I obtain:
I would like to have the background in place of the black space.
Any idea?
How about [self.tableView setBackgroundView:backgroundView];?
Bear with me I'm a beginner. I just ran into this problem and I found two solutions. In my case, I was using a UITableView object.
To begin, add the background image into your app's directory. I put mine in "Supporting Files"
You can add the image as a UIColor object:
...
[resultsTable setBackGroundColor:setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"background.jpg"]]];
...
The problem with this method is that it will repeat your background image because it's treating it as a pattern.
The better way:
// Create a string to store the path location of your image, otherwise you would have to provide an exact path name
NSString *backPath = [[NSBundle mainBundle] pathForResource:#"background"
ofType:#"jpg"];
// Create a UIImage Object to store the image.
UIImage *bgImage = [[UIImage alloc ] initWithContentsOfFile:backPath];
// Create a UIImageView which the TableView
UIImageView *bgView = [[UIImageView alloc]initWithImage:bgImage];
// Set your TableView's background property, it takes a UIView Obj.
[resultsTable setBackgroundView: bgView];
.....

UINavigationBar with image and default gradient background with iOS5

I'm attempting to use the new [UINavigationBar appearance] functionality in iOS5 to add a logo image to the UINavigationBars in my application. Primarily, I'd like to keep the default gradient, but center a transparent png in the NavBar. The logo image is roughly 120 pixels wide (240 pixels#2x).
I have first attempted this by setting the background image. The default behavior for setBackgroundImage:forBarMetrics: appears to be to tile the image, and all transparent parts show the default navbar background color, black. I can also set the background color via the appearance modifier, and get a flat color background, but I'd really like to get the original gradient behavior without maintaining a separate image resource for it. It also makes it easier to adjust in code, since I can adjust the tint there, rather than re-generating a new image if I decide to change it.
What I'm trying to use:
UIImage *logoImage = [UIImage imageNamed:#"logoImage"];
[[UINavigationBar appearance] setBackgroundImage:logoImage forBarMetrics:UIBarMetricsDefault];
You can do this two ways. If you want to always have the image in the navigation bar, then create an image view and set it as a subview of the navigation bar:
[self setLogoImageView:[[UIImageView alloc] init]];
[logoImageView setImage:[UIImage imageNamed:#"logo.png"]];
[logoImageView setContentMode:UIViewContentModeScaleAspectFit];
CGRect navFrame = [[navController navigationBar] frame];
float imageViewHeight = navFrame.size.height - 9;
float x_pos = navFrame.origin.x + navFrame.size.width/2 - 111/2;
float y_pos = navFrame.size.height/2 - imageViewHeight/2.0;
CGRect logoFrame = CGRectMake(x_pos, y_pos, 111, imageViewHeight);
[logoImageView setFrame:logoFrame];
[[[self navigationController] navigationBar] addSubview:logoImageView];
If you only want to display the logo in a certain view, then set the view's navigation item:
[logoImageView setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
[[self navigationItem] setTitleView:logoImageView];

Resources