set alpha background image iphone application in xcode - iphone

I got this:
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"bg.png" ]];
and some other stuff like textfields and stuff on one view.
I want to set the backgroundimages alpha so that it doesnt distract.
but when i do:
self.view.alpha
it sets it for all objects :-(
and there is no:
self.view.backgroundcolor.alpha
any idea?

I worked it out so that you don't need 2 separate views.
// Make sure your whole view is NOT transparent
self.view.alpha = 1.0f;
// Use UIColor to set alpha transparency of just the background view
self.view.backgroundColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.5f];
Voila!

That's how it works. To achieve the effect you desire, you need two separate views, one that contains just the background (and has its alpha set), and one with a transparent background and alpha of 1.0 that contains the other controls.

Related

Table Cell background and text color cannot be set accurately.

I need to set the background of table cells to a specific color.
(#222222 or RGB(32,32,32) respectively)
The background of the table view in IB is set properly. The correct gray appears in the back of the table header and in section headers etc.
But I struggle with the cells.
To customize the cell's apperance I subclass UITableCell and implement the layoutSubviews method.
This works fine:
- (void)layoutSubviews {
[super layoutSubviews];
self.selectionStyle = UITableViewCellSelectionStyleGray;
self.backgroundColor = [UIColor darkGrayColor];
self.contentView.backgroundColor = [UIColor darkGrayColor];
self.textLabel.textColor = [UIColor whiteColor];
self.detailTextLabel.textColor = [UIColor grayColor];
}
However, grayColor and darkGrayColor simply do not match the colour that I need.
Naturally I tried the colorWithRed:green:blue:alpha method of UIColor.
- (void)layoutSubviews {
[super layoutSubviews];
self.selectionStyle = UITableViewCellSelectionStyleGray;
self.backgroundColor = [UIColor colorWithRed:(32/256) green:(32/256) blue:(32/256) alpha:1];
self.contentView.backgroundColor = [UIColor colorWithRed:(32/256) green:(32/256) blue:(32/256) alpha:1];
self.textLabel.textColor = [UIColor whiteColor];
self.detailTextLabel.textColor = [UIColor colorWithRed:(32/256) green:(32/256) blue:(32/256) alpha:1];
}
That one results in black background and black color of the detailTextLable.
(Of course it is senseless using the same color for a background and for a text label. I am just trying to work out what colorWithRed:green:blue:alpha does and does not.)
With plain style tables I am fine. Those' cells do not have a background color at all. When I just omit setting the backgroundColor and the contentView's background Color propierties then the background of the cells is displayed as defined as the Table's background color in IB.
But with grouped tables the standard background is some light gray which I want to change to some more decent color that matches my client's style guide.
What am I doing wrong? Do I use colorWithRed:green:blue:alpha properly?
Any suggestion is much appreciated.
I would try an other methods of calucaluting the color float:
[UIColor colorWithRed:(32.0f/255.0f) green:(32.0f/255.0f) blue:(32.0f/255.0f) alpha:1.0f];
since 0 is also include you have 0 to 255 values not 1 to 256 values.
If you want the cell to be transparent use [UIColor clearColor]
32/256 = 0 but 32/256.0 = 0.125.
Thanks for putting your finger to the error.
However, this does not exactly answer the question related to the difference between plain and grouped table style.
Just in case somebody finds the question interesting:
The grouped style comes with a background view. I set that to nil.
The gropued style always has a cell background color set to something. Therefore omitting the self.backgroundColor=... statement was not sufficient.
finally
self.backgroundColor = [UIColor clearColor];
did the trick. It was just not remotely connected to the objective-c mistake that I made too.

How to preserve image transparency when using colorWithPatternImage:

I have an image prepared with transparency, like this:
With two UIviews, I configure the background colors as so:
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
self.dashedView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"red_square.png"]];
I expect the red square to be repeated over the UIView with transparency preserved, but it's been filled by a solid color like this:
I don't understand why. Is there a simple way to draw a tiled image with transparency? Or do I need to look at drawing Core Graphics patterns?
Pattern images should be keeping the transparency just fine.
Try [self.dashedView setOpaque:NO]
all you have to do is set opaque to NO after setting the colorWithPatternImage.
self.dashedView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"red_square.png"]];
self.dashedView.opaque = NO;
Yar, thanks for the feedback.
I found this only happens on iOS 4.3.x but not on iOS 5.0.x. So on 4.3.x I had to do what Yar id and set opaque to NO, then set background image, then set to YES.
UIButton* cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
cancelButton.opaque = YES; // fix for strange issue in 4.x, need to set YES, set bg color image, then set back to NO
cancelButton.backgroundColor = [UIColor colorWithPatternImage: cancelImage];
cancelButton.opaque = NO;

iPhone - UIView's backgroundColor - using a png with transparency?

I'm setting the background color of my UIView (which is within a UIViewController) as follows:
myView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"myTransparentBG.png"]];
I've even tried messing with [myView setOpaque:NO]; but the image doesn't appear transparent. It has a black background to it. Am I abel to fix this programmatically? Otherwise, how are we supposed to set a transparent background to our view?
This seems like a question that should have been asked before, but I couldn't find an answer.
When using a transparent pattern, after setting the backgroundColor property, you need to set opaque property to NO on both view and its layer. Here's a sample:
UIView* paperView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,700)];
paperView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"paper.png"]];
[paperView.layer setOpaque:NO];
paperView.opaque = NO;
I haven't ever actually done this, but here's what I'd try.
Set the UIView's .backgroundColor to [UIColor clearColor]. Then put a UIImageView containing your PNG as the "lowest" item on the UIView's stack of subviews. Make sure the UIImageView's background color is also clear.
Use this: myView.backgroundColor = [UIColor clearColor];
This will set it to a transparent background.

iphone uiwebview initial white view

My app's main view has a uiwebview. It is white for a split second before it has time to render the HTML the app generates.
Is there a way to make the uiwebview black, or another color, before it renders? The flash of white isn't part of my plan for a smooth visual transition.
Objective-C or MonoTouch answers are fine, I am bi-lingual.
Another thing to try is to make the view transparent:
webView.backgroundColor = [UIColor clearColor];
webView.opaque = NO;
This causes additional compositing work, however, so you can reset these values to something more friendly for that after your web view loads, in webViewDidFinishLoad:.
- (void) webViewDidFinishLoad:(UIWebView *)webView
{
webView.backgroundColor = [UIColor blackColor];
webView.opaque = YES;
}
One thing you can try is put a UIView with the color you want and position it above the UIWebView with the same width and height. Then in the webViewDidFinishLoad method, set the UIView to hidden.
I can't comment yet hence the answer.
#Steve, actually it's possible to do with storyboards.
Place a UIView under the UIWebView and:
// Can be set in the storyboard
bottomView.backgroundColor = [UIColor yellowColor];
webview.alpha = 0;
Keep in mind that a lot of page fetch stuff after being loaded via JS so you still can be left with a white page couple seconds after
- (void) webViewDidFinishLoad:(UIWebView *)webView
{
webView.alpha = 1;
}
To add to Steve Madsen's answer (since I can't comment yet). If you instanced the UIWebView in Interface Builder, you wont be able to set alpha to 0.0, but what you can do is bring up the color picker to set a background color (white, black, gray, it doesnt matter), then set the opacity of that color to zero percent, and that works.

How do I set a background image for a grouped table view?

I've seen some iPhone applications that use a custom image as the background for a grouped UITableView, instead of the standard gray lines.
How is this achieved?
Here's what worked for me (and fairly simple once I figured it out ;)
1) Add a view in your app delegate and make it a subview of the window:
UIView *bgView = [[UIView alloc]initWithFrame:window.frame];
bgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"screenBG.png"]];
[window addSubview:bgView];
[bgView release];
2) On each view controller .m file, under ViewDidLoad, set background color of that particular view to transparent (so the other bgView created above will show through):
self.view.backgroundColor = [UIColor clearColor];
And in my case, the view controller in step 2 was a tableviewcontroller. Looks great.
And BTW, doing the following in each view controller did NOT work well:
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"screenBG.png"]];
So follow steps 1 and 2 above.
Hope this helps out,
Tbone
Try this
- (void) viewDidLoad {
[super viewDidLoad];
self.tableView.backgroundColor = [UIColor clearColor];
self.view.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:#"wallpaper.png"]];
self.tableView.opaque = NO;
}
In another project (developed using 2.2.1) I did this by setting my UITableView's background opacity to 0%, and then simply layering a UIImageView behind it using Interface Builder. This allowed me to have a fixed background regardless of the table state. You can also set the background of the UITableView to be an image instead, but then the background scrolls with the table. (I don't have the code handy at the moment, but I got the tip a while back on the Apple developer forums).
Note that this can cause some performance issues. Apple discourages using transparency whenever possible because the GPUs on the pre-3GS models aren't particularly beefy.
You can use the +[UIColor colorWithPatternImage:(UIImage)] method like so:
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Background.png"]];
the problem with colorWithPatternImage: is that you need to use "patterned" images, otherwise your image will be tiled randomly
this link has a simple solution, if you want all your views to have the same background
http://howtomakeiphoneapps.com/2009/03/how-to-add-a-nice-background-image-to-your-grouped-table-view/
self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"SortByCategory_320x480.png"]];
self.tableView.separatorColor = [UIColor clearColor];
self.tableView.backgroundColor = [UIColor clearColor];
Hope this will help. It won't display hideous translucent background behind the cells especially in case of Grouped UITableView.