How to set the full width of separator in UITableView
I got the answer from this post: iOS 8 UITableView separator inset 0 not working
Just add this code on your UITableViewController
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[self.tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[self.tableView setLayoutMargins:UIEdgeInsetsZero];
}
}
Can you change separator width for different tableview cells?
You can change the dimensions of the separator using cell.separatorInset
.
For instance this draws a shorter separator for the first three cells:
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row < 3 {
cell.separatorInset = UIEdgeInsetsMake(0, 30, 0, 11) // shorter separator
else {
cell.separatorInset = UIEdgeInsetsMake(0, 3, 0, 11) // longer separator
}
}
From the willDisplayCell
docs:
A table view sends this message to its delegate just before it uses cell to draw a row, thereby permitting the delegate to customize the cell object before it is displayed. This method gives the delegate a chance to override state-based properties set earlier by the table view, such as selection and background color. After the delegate returns, the table view sets only the alpha and frame properties, and then only when animating rows as they slide in or out.
uitableviewcell separator line not displaying in full width
Hey All I got the solution.
I override layoutSubviews in my Custom UITableViewCell and the separators are displayed properly.
- (void)layoutSubviews {
[super layoutSubviews];
for (UIView *subview in self.contentView.superview.subviews) {
if ([NSStringFromClass(subview.class) hasSuffix:@"SeparatorView"]) {
subview.hidden = NO;
}
} }
Separator lines for UITableViewCellStyleSubtitle cells not taking the full width
Add Table View Separator Insets to Zero. Not Just for Table View Cell.
Add this code in cellForRowAtIndexPath
:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//....
if ([[[UIDevice currentDevice]systemVersion]floatValue]>=8.0)
{
cell.layoutMargins = UIEdgeInsetsZero;
cell.preservesSuperviewLayoutMargins = NO;
}
}
Integration with your code :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];
if ([[[UIDevice currentDevice]systemVersion]floatValue]>=8.0)
{
cell.layoutMargins = UIEdgeInsetsZero;
cell.preservesSuperviewLayoutMargins = NO;
}
NSDictionary* item = [self.items objectAtIndex:indexPath.row];
cell.textLabel.text = item[@"title"];
cell.detailTextLabel.text = item[@"subtitle"];
UIImage* icon = [UIImage imageNamed:item[@"icon"]];
[cell.imageView setImage:icon];
return cell;
}
See the preview :
specify the separator line width of the table view
to remove all margin settings from the cell. try adding all these to the cell in
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
// Remove insets in UITableViewCell separator
// Remove seperator inset
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
cell.separatorInset = UIEdgeInsetsZero;
}
// Prevent the cell from inheriting the Table View's margin settings
if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
cell.preservesSuperviewLayoutMargins = NO;
}
// Explictly set cell's layout margins
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
cell.layoutMargins = UIEdgeInsetsZero;
}
Separator line not showing full width in iPad
you can directly changed in your Attribute Inspector change separator Inset --> custom and set left --> 0
and in your cell class also change the layout margin
Update
if want to remove the separator inset from all cells, you need to do two things. First, add these two lines of code to your table view controller's viewDidLoad() method:
override func viewDidLoad() {
super.viewDidLoad()
tableView.layoutMargins = .zero
tableView.separatorInset = .zero
}
Now look for you cellForRowAt method and add this:
cell.layoutMargins = .zero
Is it possible to increase the UITableView separator line thickness?
The only way of doing it, is setting the separtorStype to UITableViewCellSeparatorStyleNone and then you have two options:
- Create a custom UITableViewCell with the separator inside it or
- Create an alternate UITableViewCell with the separator you want and place inside every other cells. If you want to display 3 rows on your table, you should display 5 instead with the alternate cell in rows 2 and 4.
cell separator line in uitableview not covering the complete width
With this codes it will be ok :
In method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
:
cell.layoutMargins = UIEdgeInsetsZero;
cell.preservesSuperviewLayoutMargins = NO;
EDIT :
Changed code lines according to this post : iOS 8 UITableView separator inset 0 not working
iOS7 last cell in UITableView section forces full width separator
After some experimentation I've found the only real solution is to set the UITableViewStyle
to UITableViewStylePlain
and set empty footers using:
-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
return [[UIView alloc] initWithFrame:CGRectZero];
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 0.01f;
}
This will not be a satisfactory solution for some, because UITableViewStylePlain
does not provide all of the features of UITableViewStyleGrouped
, but it gives me the section without the full-width separator.
Related Topics
Swift - Using Replacerange() to Change Certain Occurrences in a String
Change Lock Screen Background Audio Controls Text
Decode Base-64 Encoded Png in an Nsstring
I Get Conflicting Provisioning Settings Error When I Try to Archive to Submit an iOS App
Uicollectionview - Horizontal Scroll, Horizontal Layout
Scale Image in an Uibutton to Aspectfit
What Is the Bundle Identifier of Apple's Default Applications in iOS
How to Enable Arc Project-Wide in Xcode 4.2
How to Make Reorder Control of Uitableviewcell in Left Side
Reverse an Audio File Swift/Objective-C
How to Use Sfsafariviewcontroller with Swiftui
How to Set Image in Tabbar Not Tint Color in iOS
Images Can't Contain Alpha Channels or Transparencies
Disable Autolayout Constraint Error Messages in Debug Console Output in Xcode
iOS Convert Large Numbers to Smaller Format
Checking a Null Value in Objective-C That Has Been Returned from a JSON String