Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 448 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
iOS 8 UITableView separator inset 0 not working

#41
## After much investigation...
Here's the only way to fully control this stuff (that I could find)

To fully control both separator insets and layout margins on each cell. Do this in the `willDisplayCell` method on your `UITableviewDelegate`.

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.layoutMargins = UIEdgeInsetsZero
cell.contentView.layoutMargins = UIEdgeInsetsMake(0, 10, 0, 10)
cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
}

The cell object controls the separator, and the `contentView` controls everything else. If your separator inset spaces are showing up in an unexpected color this should solve it:

cell.backgroundColor = cell.contentView.backgroundColor
Reply

#42
Just override the "cellForRowAtIndexPath" and set "cell.preservesSuperviewLayoutMargins = false" and "cell.separatorInset = UIEdgeInsets.zero" and "cell.layoutMargins = UIEdgeInsets.zero"

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell: LayoutTableViewCell! = tableView.dequeueReusableCell(withIdentifier: "LayoutCell") as? LayoutTableViewCell



let currentLayout = OrderLayouts()[indexPath.row]

cell.NameLabel?.text = currentLayout.name
cell.DescrLabel?.text = currentLayout.descr

if(GlobalVariables.debug){

cell.DescrLabel?.text = "\(currentLayout.id) \n \(currentLayout.descr)"

}

cell.preservesSuperviewLayoutMargins = false
cell.separatorInset = UIEdgeInsets.zero
cell.layoutMargins = UIEdgeInsets.zero


return cell

}
Reply

#43
**iOS introduces the layoutMargins property on cells AND table views.**

This property isn't available in iOS 7.0 so you need to make sure you check before assigning it!

However, Apple has added a **property** called **preservesSuperviewLayoutMargins** to your cell that will prevent it from inheriting your Table View's margin settings. This way, your cells can configure their own margins independently of the table view. Think of it as an override.

This property is called *preservesSuperviewLayoutMargins*, and setting it to NO can allow you to override your Table View's layoutMargin settings with your own cell's layoutMargin setting. It both saves time (**you don't have to modify the Table View's settings**), and is more concise. Please refer to Mike Abdullah's answer for a detailed explanation.

*NOTE: this is the proper, less messy implementation, as expressed in Mike Abdullah's answer; setting your cell's preservesSuperviewLayoutMargins=NO will ensure that your Table View does not override the cell settings.*


**First step - Setup your cell margins:**

/*
Tells the delegate that the table view is about to draw a cell for a particular row.
*/
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,
forRowAtIndexPath indexPath: NSIndexPath)
{
// Remove separator inset
if cell.respondsToSelector("setSeparatorInset:") {
cell.separatorInset = UIEdgeInsetsZero
}

// Prevent the cell from inheriting the Table View's margin settings
if cell.respondsToSelector("setPreservesSuperviewLayoutMargins:") {
cell.preservesSuperviewLayoutMargins = false
}

// Explictly set your cell's layout margins
if cell.respondsToSelector("setLayoutMargins:") {
cell.layoutMargins = UIEdgeInsetsZero
}
}

Setting the preservesSuperviewLayoutMargins property on your cell to NO **should** prevent your table view from overriding your cell margins. In some cases, it seems not to function properly.

**Second step - Only if all fails, you may brute-force your Table View margins:**

/*
Called to notify the view controller that its view has just laid out its subviews.
*/
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()

// Force your tableview margins (this may be a bad idea)
if self.tableView.respondsToSelector("setSeparatorInset:") {
self.tableView.separatorInset = UIEdgeInsetsZero
}

if self.tableView.respondsToSelector("setLayoutMargins:") {
self.tableView.layoutMargins = UIEdgeInsetsZero
}
}

...and there you go! This should work on iOS 8 as well as iOS 7.

Note: tested using iOS 8.1 and 7.1, in my case I only needed to use the first step of this explanation.

The Second Step is only required if you have unpopulated cell beneath the rendered cells, ie. if the table is larger than the number of rows in the table model. Not doing the second step would result in different separator offsets.
Reply

#44
**iOS 8.0 introduces the layoutMargins property on cells AND table views.**

This property isn't available on iOS 7.0 so you need to make sure you check before assigning it!

The easy fix is to subclass your cell and override the layout margins property as suggested by @user3570727. However you will lose any system behavior like inheriting margins from the Safe Area so I do not recommend the below solution:

(ObjectiveC)

-(UIEdgeInsets)layoutMargins {
return UIEdgeInsetsZero // override any margins inc. safe area
}

(swift 4.2):

override var layoutMargins: UIEdgeInsets { get { return .zero } set { } }

If you don't want to override the property, or need to set it conditionally, keep reading.


----------


In addition to the `layoutMargins` property, Apple has added a **property** to your cell that will prevent it from inheriting your Table View's margin settings. When this property is set, your cells are allowed to configure their own margins independently of the table view. Think of it as an override.

This property is called `preservesSuperviewLayoutMargins`, and setting it to `NO` will allow the cell's `layoutMargin` setting to override whatever `layoutMargin` is set on your TableView. It both saves time (**you don't have to modify the Table View's settings**), and is more concise. Please refer to Mike Abdullah's answer for a detailed explanation.


*NOTE: what follows is a clean implementation for a **cell-level margin setting**, as expressed in Mike Abdullah's answer. Setting your cell's `preservesSuperviewLayoutMargins=NO` will ensure that your Table View does not override the cell settings. If you actually want your entire table view to have consistent margins, please adjust your code accordingly.*

**Setup your cell margins:**

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
// Remove seperator inset
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}

// Prevent the cell from inheriting the Table View's margin settings
if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
[cell setPreservesSuperviewLayoutMargins:NO];
}

// Explictly set your cell's layout margins
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}


**Swift 4:**

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
// Remove seperator inset
if cell.responds(to: #selector(setter: UITableViewCell.separatorInset)) {
cell.separatorInset = .zero
}
// Prevent the cell from inheriting the Table View's margin settings
if cell.responds(to: #selector(setter: UITableViewCell.preservesSuperviewLayoutMargins)) {
cell.preservesSuperviewLayoutMargins = false
}
// Explictly set your cell's layout margins
if cell.responds(to: #selector(setter: UITableViewCell.layoutMargins)) {
cell.layoutMargins = .zero
}
}

Setting the `preservesSuperviewLayoutMargins` property on your cell to NO **should** prevent your table view from overriding your cell margins. In some cases, it seems to not function properly.

**If all fails, you may brute-force your Table View margins:**

-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];

// Force your tableview margins (this may be a bad idea)
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[self.tableView setSeparatorInset:UIEdgeInsetsZero];
}

if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[self.tableView setLayoutMargins:UIEdgeInsetsZero];
}
}


**Swift 4:**

func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// Force your tableview margins (this may be a bad idea)
if tableView.responds(to: #selector(setter: UITableView.separatorInset)) {
tableView.separatorInset = .zero
}
if tableView.responds(to: #selector(setter: UITableView.layoutMargins)) {
tableView.layoutMargins = .zero
}
}

...and there you go! This should work on iOS 7 and 8.


----------


**EDIT:** **Mohamed Saleh brought to my attention a possible change in iOS 9.** You may need to set the Table View's `cellLayoutMarginsFollowReadableWidth` to `NO` if you want to customize insets or margins. Your mileage may vary, this is not documented very well.

This property only exists in iOS 9 so be sure to check before setting.

if([myTableView respondsToSelector:@selector(setCellLayoutMarginsFollowReadableWidth:)])
{
myTableView.cellLayoutMarginsFollowReadableWidth = NO;
}

**Swift 4:**

if myTableView.responds(to: #selector(setter: self.cellLayoutMarginsFollowReadableWidth)) {
myTableView.cellLayoutMarginsFollowReadableWidth = false
}

(above code from

[To see links please register here]

)


**EDIT: Here's a pure Interface Builder approach:**

[![TableViewAttributesInspector][1]][1]
[![TableViewCellSizeInspector][2]][2]


[1]:

[2]:



*NOTE: iOS 11 changes & simplifies much of this behavior, an update will be forthcoming...*
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through