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;
UITableViewCell
selectedBackgroundView
to be the UIView
that I created with my chosen background colourThis worked well for me. Thanks for the tip Zonble.
UITableViewCell
has three default selection styles:-
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With