How to change the blue highlight color of a UITableViewCell?
I'm wonderin开发者_运维问答g how to change the blue highlight/selection color of a UITableViewCell
, any ideas?
You can change the highlight color in several ways.
Change the selectionStyle property of your cell. If you change it to
UITableViewCellSelectionStyleGray
, it will be gray.Change the
selectedBackgroundView
property. Actually what creates the blue gradient is a view. You can create a view and draw what ever you like, and use the view as the background of your table view cells.
Zonble has already provided an excellent answer.
I thought it may be useful to include a short code snippet for adding a UIView
to the tableview cell that will present as the selected background view.
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
UIView *selectionColor = [[UIView alloc] init];
selectionColor.backgroundColor = [UIColor colorWithRed:(245/255.0) green:(245/255.0) blue:(245/255.0) alpha:1];
cell.selectedBackgroundView = selectionColor;
- cell is my
UITableViewCell
- I created a UIView and set its background color using RGB colours (light gray)
- I then set the cell
selectedBackgroundView
to be theUIView
that I created with my chosen background colour
This worked well for me. Thanks for the tip Zonble.
UITableViewCell
has three default selection styles:-
- Blue
- Gray
- None
Implementation is as follows:-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath {
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
In Swift, use this in cellForRowAtIndexPath
let selectedView = UIView()
selectedView.backgroundColor = .white
cell.selectedBackgroundView = selectedView
If you want your selection color be the same in every UITableViewCell
,
use this in AppDelegate
.
let selectedView = UIView()
selectedView.backgroundColor = .white
UITableViewCell.appearance().selectedBackgroundView = selectedView
If you want to change it app wide, you can add the logic to your App Delegate
class AppDelegate: UIResponder, UIApplicationDelegate {
//... truncated
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
// set up your background color view
let colorView = UIView()
colorView.backgroundColor = UIColor.yellowColor()
// use UITableViewCell.appearance() to configure
// the default appearance of all UITableViewCells in your app
UITableViewCell.appearance().selectedBackgroundView = colorView
return true
}
//... truncated
}
for completeness: if you created your own subclass of UITableViewCell
you can implement the - (void)setSelected:(BOOL)selected animated:(BOOL)animated
method, and set the background color of some view you added in the content view. (if that is the case) or of the contentView itself (if it is not covered by one of your own views.
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
if(selected) {
self.contentView.backgroundColor = UIColor.blueColor;
} else {
self.contentView.backgroundColor = UIColor.whiteColor;
}
}
(did not use ? to fit the small width of source code DIV's :)
this approach has two advantages over using selectedBackgroundView, it uses less memory, and slightly less CPU, not that u would even notice unless u display hundreds of cells.
I have to set the selection style to UITableViewCellSelectionStyleDefault
for custom background color to work. If any other style, the custom background color will be ignored. Tested on iOS 8.
The full code for the cell as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"MyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// This is how you change the background color
cell.selectionStyle = UITableViewCellSelectionStyleDefault;
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor redColor];
[cell setSelectedBackgroundView:bgColorView];
return cell;
}
@IBDesignable class UIDesignableTableViewCell: UITableViewCell {
@IBInspectable var selectedColor: UIColor = UIColor.clearColor() {
didSet {
selectedBackgroundView = UIView()
selectedBackgroundView?.backgroundColor = selectedColor
}
}
}
In your storyboard, set the class of your UITableViewCell to UIDesignableTableViewCell, on the Attributes inspector, you may change the selected color of your cell to any color.
You can use this for all of your cells. This is how your Attributes inspector will look like.
Based on @user's answer, you can just add this extension anywhere in your app code and have your selection color directly in storyboard editor for every cells of your app :
@IBDesignable extension UITableViewCell {
@IBInspectable var selectedColor: UIColor? {
set {
if let color = newValue {
selectedBackgroundView = UIView()
selectedBackgroundView!.backgroundColor = color
} else {
selectedBackgroundView = nil
}
}
get {
return selectedBackgroundView?.backgroundColor
}
}
}
Swift 3.0/4.0
If you have created your own custom cell you can change the selection color on awakeFromNib()
for all of the cells:
override func awakeFromNib() {
super.awakeFromNib()
let colorView = UIView()
colorView.backgroundColor = UIColor.orange.withAlphaComponent(0.4)
self.selectedBackgroundView = colorView
}
1- Add a view to the content view of your cell.
2- Right click your cell.
3- Make the added view as "selectedBackgroundView"
Swift 5
Another solution is to just override the set selected method on your UITableViewCell
.
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
let selectionView = UIView()
selectionView.backgroundColor = UIColor.green
// selectionView.alpha = 0.3 // optional if you want to tone down a colour
self.selectedBackgroundView = selectionView
}
Note: Works in Mac Catalyst.
精彩评论