I have added an UITableView to Main.storyboard without setting any additional attributes. On the iPhone the table fills out in the portrait as well as in the landscape mode, the complete screen, which is the desired behavior. However, when starting the app on an iPad, the table looks like this:
When I add the following constraints to the table view:
Leading Space to Container Margin
Trailing Space to Container Margin
Vertical Spacing to Top Layout Guide
Vertical Spacing to Bottom Layout Guide
The issue is fixed on the iPad (portrait & landscape mode), but on the iPhone the first cell in the landscape mode is hidden:
How can I fix this? This issue does not occur in the portrait mode on the iPhone, nor does it happen on the iPad in both modes.
BTW, the controller attribute "Adjust Scroll View Insets" is enabled.
For the inset please add this below code
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if ([tableView respondsToSelector:#selector(setSeparatorInset:)]) {
[tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([tableView respondsToSelector:#selector(setLayoutMargins:)]) {
[tableView setLayoutMargins:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:#selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
For making the tableView fit in all devices so it fits in the whole screen add the below shown constraints.
The resulting TableView will be
Related
I knew this question is old, so many answers was already posted here.
The problem is, the solutions are working fine with iPhone's. But when trying it with iPad, I still faces some issue.
Refer the below images(First one is portrait mode and the second one is in landscape mode).
I used the below code in my view controller where I wrote table view's delegate methods.
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if ([tableView respondsToSelector:#selector(setSeparatorInset:)]) {
[tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([tableView respondsToSelector:#selector(setLayoutMargins:)]) {
[tableView setLayoutMargins:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:#selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
Question:
Is there is any way to remove these left empty space of table view's separator in iPad? Should I include any extra check's beyond the iPhone's working code?
Confused!!
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;
}
i am working in a project in which i am using UITableView to display data. The orientation of my project is Landscape. now i want to add portrait orientation mode to my project so that any user can use app in portrait as well as in landscape mode.
Is there any code or method available by the help of which i will change the orientation of UITableView from horizontal to vertical??
i want the orientation something like this
Just develop 2 different prototypes with unique identifiers, provide them in cellForRowAtIndexPath based on current orientation.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {
LatestNewsCell *cell = (LatestNewsCell *)[tableView dequeueReusableCellWithIdentifier:#"portraitCell" forIndexPath:indexPath];
// configure cell
return cell;
} else {
LatestNewsCell *cell = (LatestNewsCell *)[tableView dequeueReusableCellWithIdentifier:#"landscapeCell" forIndexPath:indexPath];
// configure cell
return cell;
}
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[self.tableView reloadData];
}
In the case of the cell with more labels, you just create additional IBOutlets for these and reuse the existing ones for the labels that are used in both orientations.
I believe you just need to setup your project to support portrait orientation. After that, your UITableView will rotate as you rotate your device or simulator.
I have created a simple project to test this. Check the output here.
Notice that sometimes, the view correctly rotates, and sometimes not. Must be a bug.
Apple has deprecated margins in table views, since iOS7, but designers still seem to want margins. What’s the best way t implement margins in iOS 7 and above? The old “group” table view style that included margins in iOS 6 no longer indents the left and right edges.
I have aUITableView subclass, in which i’m using the following method to programmatically add margins to the edge of all subviews, except thebackgroundView (such as cells, section header etc):
Code:
- (void)layoutSubviews {
[super layoutSubviews];
for (UIView *subview in self.subviews) {
if (subview != self.backgroundView && subview.frame.origin.x == 0.0 && subview.frame.size.width == self.frame.size.width) {
subview.frame = CGRectInset(subview.frame, 10.0, 0);
}
}
}
During runtime, it all seems to work as desired. The cells resize according to the margin offset and the subviews of those cells adjust correctly.
However, in the storyboard, usingIBDesignable, the cells adjust correctly but the cells’ subviews do not seem to resize within the cell’s calculated size
Is there a better way to implement margins in aUITableView?
Thanks c:
UIEdgeInsets is used to implement margins around the tableview that looks like grouped tableview. You may try below solutions and check it.
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if ([tableView respondsToSelector:#selector(setSeparatorInset:)]) {
[tableView setSeparatorInset:UIEdgeInsetsZero];//iOS7
}
if ([tableView respondsToSelector:#selector(setLayoutMargins:)]) {
[tableView setLayoutMargins:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:#selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
//OR
-(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];
}
}
For iOS 8 use
- (UIEdgeInsets)layoutMargins {
return UIEdgeInsetsZero;
}
Hope it helps you..
Apple switched to layout margins in iOS 8. The advantage of adding constraints to margins instead of edges, is that your margin is now adaptive, and will adjust itself, based on the screen's traits.
After I transitioned to constraining to margins (instead of edges), I really appreciated how it simplified layout, and eliminating having to adjust constraints myself. It does the work for me, and it looks pleasing.
I am using xcode6.0.1.In my UIVIew Controller contains one tableview and i need to set separator inset full width.I changed my tableview separator inset via xib.But i can’t get full width for separator inset on my xcode6 (In my xcode5 separator inset full width is possible).How to solve this issue?
change tableview separator inset via xib
my tableview separator line like this(not get full width)
I solved my issue
-(void)viewDidLoad{
self.tableView.layoutMargins = UIEdgeInsetsZero;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if ([cell respondsToSelector:#selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
Use this method in iOS 8 [tableView setLayoutMargins:UIEdgeInsetsZero];
I guess you simulated your app in iOS8 , this is a new property "#property(nonatomic) UIEdgeInsets layoutMargins " in iOS8. I suggest you try to create a UITableViewCell class category(e.g. UITableViewCell+Separator) then add this getter
- (UIEdgeInsets)layoutMargins
{
return UIEdgeInsetsZero;
}
in iOS7 this will not be called cos there's no this property in SDK,and will not cause any crash; in iOS8 this will be called every time you use the cell
It works for me
Here's how to reproduce this strange behavior in Xcode 4.2.1:
(1) Create a new iPad application for iOS 5 using the built-in Master-Detail Template. Make sure "using ARC" and "Use storyboard" are checked.
(2) In your storyboard, add a UITextView to the UIView in your Detail View Controller, just under the label that says "Detail view content goes here".
(3) Also in your storyboard, select the Master's UITableView and in the Attributes Inspector, set the Content to "Dynamic Prototypes".
(4) Again in your storyboard, select the Master's UITableview prototype cell and in the Attributes Inspector, set the Style to "Basic" and the Identifier to "CellIdentifier".
(5) For the Master View Controller implementation (MasterViewController.m), add this code:
// show only one section in the master table view
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// show 30 rows in the master table view
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 30;
}
// when showing a cell, include it's indexPath row in its title text
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// dequeue cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
// set text
cell.textLabel.text = [NSString stringWithFormat:#"Row at Index %i", indexPath.row];
// return cell
return cell;
}
(6a) Run the app. Open it in a portrait orientation, and click inside the text box (UITextView) you created on the detail view. The keyboard will open. Now rotate the device to the left into a landscape orientation.
(6b) If you scroll around the master view with the keyboard open, you will see that the master view allows you to scroll beyond the bottom row (see screenshot below). This is because the device automatically set the Master's UITableView Bottom ContentInset to -608. Closing the keyboard makes this go away.
Screenshot:
One final note: if you register for device rotation notifications in the app, and set the content insets to UIEdgeInsetsZero after the device was rotated, it still won't work: immediately after device rotation the system still has a 0 bottom inset; the -608 mysteriously gets set after this.