Change background on a UITableView embedded in a UIViewController on iOS 5 - ios

I am trying to change the grey gradient background on an embedded UITableView to the color I have set on the parent view in the Storyboard.
I have been looking around, and found the following threads:
Change iPhone tableview (style grouped) background color while preserving texture
How can I set the background of UITableView (the tableview style is “Grouped”) to use an image?
UITableView backgroundColor always gray on iPad
I have an IBOutlet in the parent controller connecting the two views.
My implementation looks as follows:
- (void)viewDidLoad {
[super viewDidLoad];
[activeShipmentsTable setBackgroundView:nil];
[activeShipmentsTable setBackgroundView:[[[UIView alloc] init] autorelease]];
[activeShipmentsTable setBackgroundColor:UIColor.clearColor];
}
What am I doing wrong?
Thanks.

Try setting et the background color on the table's background view, not the table itself.
UIView *view = [[[UIView alloc] init] autorelease];
view.backgroundColor = [UIColor redColor];
self.tableView.backgroundView = view;

Related

Instead of UICollectionView a black screen is displayed

I'm trying to reimplement the infinitive scrolling UICollectionView seen here. Things that were missing for me:
ViewController.h:
#interface ViewController : UIViewController<UICollectionViewDataSource, UICollectionViewDelegate>
#end
DataCell.h:
#interface DataCell : UICollectionViewCell
#property (nonatomic, strong) UILabel *label;
#end
DataCell.m:
#import "DataCell.h"
#implementation DataCell
-(instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if(self){
self.label = [[UILabel alloc] initWithFrame:self.bounds];
self.autoresizesSubviews = YES;
self.label.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight);
self.label.textAlignment = NSTextAlignmentCenter;
self.label.adjustsFontSizeToFitWidth = YES;
[self addSubview:self.label];
}
return self;
}
#end
CustomCollectionView.h:
#interface CustomCollectionView : UICollectionView
#end
For the whole project I used a storyboard and a normal UIViewController. On this view controller I added a UICollectionView in Interface Builder. I connected the outlet from the collection view with my view controller and set up the datasource and delegate methods again to my view controller. I also set the custom class of the UICollectionViewCell and the reuse identifier in Interface Builder.
So everything should work but I only get a black screen. What I'm missing? You can download the whole project here.
You are configuring correctly the CollectionView, just that you forgot the color of the label :)
[self.label setTextColor:[UIColor whiteColor]];
Hope it helps!
You need to manually set the background color of the collection view in the storyboard.
By default it is black (although not showing that in the storyboard editor)
I had the same issue. The black screen seems to be an indicator of no data available with collection view to display. Try changing the background color of the collection view, if that changed color got to display, your collection view is working. And then add some imageview to the collection view with tag (Ex. give a value 100 with the tag value for the image view) and with the cellforItemAtIndexPath set the images to the image view.
(You can do this with custom cell. But for now, to get the collection view work, the assignment with tag for the imageview suits better)
UIImageView * ImageView = (UIImageView *)[cell viewWithTag:100];
ImageView.image = [UIImage imageNamed:[images objectAtIndex:indexPath.row]];
[self.collectionView registerNib:[UINib nibWithNibName:#"ProductCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:#"ProductCollectionViewCell"];
self.collectionView.backgroundColor = [UIColor clearColor];
it happened to me that both the collectionView and the collection view cell had transparent backgrounds
In Swift,
self.label.textColor = UIColor.whiteColor()

Change the selected cell background colour using UIAppearance

I need to change the selected cell background colour for all the cells in my app. As I know there is a way to use UIAppearance protocol for this purposes. Is it possible to realize this by the category for UITableViewCell?
Using appearance proxy you can colour all cells. Don't know if you can target specific category.
To do the colouring put following code in your AppDelegate.m file:
Put [self customCellBackground];
in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions
and somewhere at the end:
- (void)customCellBackground {
UIView *cellBackgroundView =[[UIView alloc] init];
cellBackgroundView.backgroundColor = [UIColor blackColor];
[[UITableViewCell appearance] setSelectedBackgroundView:cellBackgroundView];}
As null's answer is not for selected cell backgrounds and Armands L.'s answer did not work consistently for me (selecting cells by 'user-tap' did work, but programmatical cell selection showed strange results (like sometimes the selected background was not visible, or did not fill the cell's height properly...).
I found a custom solution that worked:
Subclass UITableViewCell
Initialize self.selectedBackgroundView in init and
Add custom UIColor property with UI_APPEARANCE_SELECTOR for custom selected background color
.h file:
#property (nonatomic) UIColor* selectedCellBackgroundColor UI_APPEARANCE_SELECTOR;
.m file:
in init method(s):
self.selectedBackgroundView = [[UIView alloc] init];
and last but not least the setter function for the color:
- (void) setSelectedCellBackgroundColor:(UIColor*) color {
_selectedCellBackgroundColor = color;
self.selectedBackgroundView.backgroundColor = color;
}
You can't do this direct to UITableViewCell, but you can do it for its contentView:
[[UIView appearanceWhenContainedIn:[UITableViewCell class], nil] setBackgroundColor:[UIColor redColor]];
Note that it will change all the subViews bg color.
Another option is writing a category or subclass the UITableViewCell with UI_APPEARANCE_SELECTOR mark, check this question:
iOS: Using UIAppearance to define custom UITableViewCell color

UIToolbar loses translucency in iOS7.1

After running a build of our app on iOS7.1 we quickly noticed that several UIToolbars that we were using lost their 'glassy' translucent appearance and became totally transparent!
I have subclassed UIToolbar to make a custom view with some text on it.
I tried changing the translucency option and a few other properties of the UIToolbar but couldn't get the effect back?
How do I get the translucent appearance back?
After researching for a while I found a few people battling this problem on some open source projects. I managed to gather that the problem is a UIToolbar must be a subview of another view - it seems you cannot subclass it directly any more.
So the solution was to make my custom view a subclass of UIView and do the following in initWithFrame (assuming a property, "toolbar" is added to the class):
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.toolbar = [[UIToolbar alloc] initWithFrame:frame];
[self addSubview:self.toolbar];
self.toolbar.barStyle = UIBarStyleBlack;
self.toolbar.translucent = YES;
[...]
}
return self;
}

Changing Background of Edit Button - UITableView iOS7

I have a UITableView which when edited looks as follows :
Is it possible to somehow change the background behind the delete symbol so that it is not white ?
I hope this help you
UIView *cellBackView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
cellBackView.backgroundColor = [UIColor clearColor];
cell.cellBackgroundView = backView;
The problem ist that contentView gets shifted to the right when you are in editing mode, and because of this all its subviews will move to the right as well.
If your background is an imageView you should not add is as subview to contentView, set it as backgroundView of the cell instead.
Since you can't setup backgroundView from interface builder I would recommend to create a custom subclass of your cell and put the background creation into awakeFromNib.
- (void)awakeFromNib {
[super awakeFromNib];
self.backgroundView = [[UIImageView alloc] initWithImage:...];
}

Setting Background Image Programmatically In A Xib

I have a XIB file with UIControl and UIScrollView elements inside of it. I would like to add a background image to the view. I tried adding an ImageView in IB but I could not get it to be present as a background and it obscured the control elements. Sending a sendViewBack message doesn't seem to do anything either.
When I create a UIImageView programmatically, it doesn't show up.
Below is the code I attempted:
Programmatic Creation
UIImage *imageBackground = [UIImage imageWithContentsOfFile:#"globalbackground"];
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:imageBackground];
[[self view] addSubview:backgroundView];
[[self view] sendSubviewToBack:backgroundView];
Dealing with the NIB file
[[self view] sendSubviewToBack:background];
where background is an IBOutlet declared in the header file and connected to the NIB's image view in IB.
Is there a step I'm missing here?
Set the frame and dont use sendSubviewToBack:. If you are working with UIImageViews you have to use [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"imageName.png"]];
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"imageBackground"]];
backgroundView.frame = self.view.bounds;
[[self view] addSubview:backgroundView];
hope this was the deal.
Don't add the image view as a subview of the scroll view, it needs to be a separate view at the top level of the hierarchy, then sent to the back of the Z-order.
You will need to set the background of your scroll view to [UIColor clearColor], and ensure that the scroll view is not marked as opaque. You can do this in code or in interface builder.
Don't use imageWithContentsOfFile and then just pass it a filename with no extension (I'm assuming .png) - this is probably returning nil. Use imageNamed: instead (you don't supply an extension in that case, iOS4 and above)
Depending on the nature of your image, you can also generate a colour with it and use that as the background colour of your scroll view. I'm assuming self.view is the scroll view:
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"globalBackground"]];

Resources