I use a tile for view background:
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]];
In simulator everything is fine, but on real device there is something like line which I want to avoid.
Related
I'm using storyboarding and the Master-Detail Core Data template in Xcode4.
I want to set the background image for my tableView on the master view controller.
To do this was simple for the iPhone, I just did this in viewDidLoad of the MasterViewController.m:
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]];
However, this doesn't work for the iPad; the default background is being shown instead in the master tableView.
I wouldn't think this was a splitViewController issue; from the perspective of the MasterViewController object, he should always have the same tableView, right? In fact, I know this is the case, because this code works on both iPad and iPhone, a line above the "backgroundColor" assignment, and this is using the same "self.tableView" object:
self.tableView.separatorColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"separator.png"]];
This is what it looks like on the iPhone, with both the separator and background image working (sorry, can't post pictures inline yet):
http://i.imgur.com/G7djz.png
And, using the same code, on the iPad:
http://i.imgur.com/8L0ZK.png
What do I have to set in order to set the background image on the master tableView?
You need to make the background clear in interface builder. Then, you need to say self.tableView.backgroundColor = [UIColor clearColor]; in your viewDidLoad. Finally, put a UIView behind it with the appropriate background color. For some reason, the background stuff is overridden in the UITableView itself.
Aha! I found the secret sauce:
UIImage *backgroundImage = [UIImage imageNamed:#"foo.png"];
UIView *backgroundView = [[UIView alloc] init];
[backgroundView setBackgroundColor:[UIColor colorWithPatternImage:backgroundImage]];
[self.tableView setBackgroundView:backgroundView];
The key is using [self.tableView setBackgroundView:...]instead of the property self.tableView.backgroundColor = ...
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.
My background color is white! Why?
Just start an new View-based app for iPad and set background color in viewDidLoad.
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
}
What is wrong. If I set it to redColor it works. Why?
groupTableViewBackgroundColor on the iPad (or at least on an iPad with iOS 4.2) is the same as clearColor, rather than the pattern used on the iPhone. You can create a "color" with your own background pattern using UIColor's colorWithPatternImage: method.
groupTableViewBackgroundColor does not work for iPad. It is iPhone only. Instead, you can use
UIColor *clr = [UIColor colorWithRed:0.875 green:0.88 blue:0.91 alpha:1];
[[self view] setBackgroundColor:clr];
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;
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.