How do I eliminate the margin on the left side of a UITableView, without creating a gap on the right?
It appears the only solution I can find is to subclass UITableViewCell, and then override the layout of the imageView and textLabel. While researching this approach I found this related answer. (I guess I didn't realize this whole time I was searching for tableview behavior that was originally in iOS 6.)
My subclass has this one and only overridden method:
- (void)layoutSubviews
{
[super layoutSubviews];
// Slide image to the left, eliminating gutter
self.imageView.frame = CGRectMake(0, 0, self.imageView.frame.size.width, self.imageView.frame.size.height);
// Slide text label to the left, and extend width by additional
// space gained by eliminating the left-most gutter
self.textLabel.frame = CGRectMake((self.imageView.frame.origin.x + self.imageView.frame.size.width + 15), 0, self.textLabel.frame.size.width + 15, self.textLabel.frame.size.height);
}
My resulting tableview now looks exactly the way I want:
How do I adjust the left margin for prototype cells in a UITableView?
You just need to set contentInset
property of the table view. You can set value according to your need.
self.tableView.contentInset = UIEdgeInsetsMake(0, -15, 0, 0);
OUTPUT RESULT
how to reduce/remove the left/right hand margin in a grouped UITableView?
Sure; just adjust the frame of the UITableView so it's a little wider than its superview and a little to the left (in the negative X direction, in other words) of its left boundary.
How to eliminate the left and right margins of a grouped UITableView
This is how you can eliminate the left and right margins of a grouped UITableView. Just subclass UITableViewCell. Note that if you want the margin to be something other than 0 just change the setFrame method to suit your needs.
//In CustomGroupedCell.h
#import <UIKit/UIKit.h>
@interface CustomGroupedCell : UITableViewCell
@property (nonatomic, weak) UITableView *myTableView;
@end
//In CustomGroupedCell.m
#import "CustomGroupedCell.h"
@implementation CustomGroupedCell
@synthesize myTableView = _myTableView;
- (void)setFrame:(CGRect)frame
{
frame = CGRectMake(- [self cellsMargin] , frame.origin.y, self.myTableView.frame.size.width + 2 *[self cellsMargin], frame.size.height);
self.contentView.frame = frame;
[super setFrame:frame];
}
- (CGFloat)cellsMargin {
// No margins for plain table views
if (self.myTableView.style == UITableViewStylePlain) {
return 0;
}
// iPhone always have 10 pixels margin
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
return 10;
}
CGFloat tableWidth = self.myTableView.frame.size.width;
// Really small table
if (tableWidth <= 20) {
return tableWidth - 10;
}
// Average table size
if (tableWidth < 400) {
return 10;
}
// Big tables have complex margin's logic
// Around 6% of table width,
// 31 <= tableWidth * 0.06 <= 45
CGFloat marginWidth = tableWidth * 0.06;
marginWidth = MAX(31, MIN(45, marginWidth));
return marginWidth;
}
@end
How to adjust left and right insets or margins for UITableView tableHeaderView?
For UITableViews without section index:
label.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
label.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
label.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor)
])
For UITableViews with section index:
label.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
label.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
label.trailingAnchor.constraint(equalTo: tableView.layoutMarginsGuide.trailingAnchor)
])
iOS 9 UITableView separators insets (significant left margin)
Okay, I have found out the solution. The only thing required for that is to set on the presenting instance of UITableView
that flag cellLayoutMarginsFollowReadableWidth
myTableView.cellLayoutMarginsFollowReadableWidth = NO;
I wanted to find some reference in the documentation but it looks like it is not ready yet, only mentioned on diff page.
As the flag was introduced in iOS 9 for the backward compatibility you should add a check before trying to set it:
if([myTableView respondsToSelector:@selector(setCellLayoutMarginsFollowReadableWidth:)])
{
myTableView.cellLayoutMarginsFollowReadableWidth = NO;
}
For Swift 2.0
you can use #available
to check iOS version.
if #available(iOS 9, *) {
myTableView.cellLayoutMarginsFollowReadableWidth = false
}
Moreover you need to compile it with Xcode 7
or above.
EDIT
Please keep in mind that this is the only required fix if your separators looked "fine" up to iOS 8, otherwise you need to change a bit more. You can find info how to do this already on SO.
How to set left and right margin for UITableView in Swift
You can do this either by setting the .layoutMargins
on the cell's contentView
(either in the cell class itself or in cellForRowAt
:
cell.contentView.layoutMargins = .init(top: 0.0, left: 23.5, bottom: 0.0, right: 23.5)
or on the table view itself:
override func viewDidLoad() {
super.viewDidLoad()
tableView.layoutMargins = .init(top: 0.0, left: 23.5, bottom: 0.0, right: 23.5)
// if you want the separator lines to follow the content width
tableView.separatorInset = tableView.layoutMargins
}
UITableViewCell in ios7 now has gaps on left and right
Adding the image to the cell.contentView
fixes the problem:
[cell.contentView addSubview:imgView];
This way you don't even have to mind the separatorInset
property.
Related Topics
Alamofireimage Disk Cache Not Working
How to Remove the Black Border of a Textfield
Masked Uivisualeffectview Does Not Work on iOS 10
My Uiviews Muck-Up When I Combine Uipangesturerecognizer and Autolayout
How to Make a Bullet List with Swift
How to Detect Which Annotation Was Selected in Mapview
Pass Data from View Controller to Child Controller in Swift
Uiimage Aspect Fill When Using Drawinrect
How to Get the Today's and Tomorrow's Date in Swift 4
Setting a Stateobject Value from Child View Causes Navigationview to Pop All Views
Why am I Getting an Error Regarding Bolts Framework and Facebooksdk When I'm Not Even Using Bolts
Unusernotificationcenter.Current().Getdeliverednotifications Only Ever Returns an Empty Array
How to Programmatically Check and Open an Existing App in iOS 8
How to Get All the Sundays in Array of Date iOS
iOS Swift 2 Record Video Avcapturesession
How to Use a Generic Class Without the Type Argument in Swift