UITableViewController's tableView's Content insets - ios

I want to make a padding for tableView's content. The result should be the following:
Result I want to achieve
If I use tableView.contentInset = UIEdgeInset(...) like this:
func setStandartDesign() {
...
tableView.contentInsetAdjustmentBehavior = .always
tableView.showsVerticalScrollIndicator = false
tableView.contentInset = UIEdgeInsets(top: 0, left: .bigOffset, bottom: 0, right: .bigOffset)
...
}
To be clear .bigOffset is merely a constant value that is equal to 12
And then use it in viewDidLoad:
override func viewDidLoad() {
setStandartDesign()
}
I have insets only on the left side of a tableView:
No inset on the right side
Alternatively I can use .insetGrouped style of the tableView. However, I do not know how to change a padding size. Is there any possible way to fix this problem?

Related

How to set space between tableView Cells?

Implemented Programmatically Custom Cell
I am trying to set space between cells but this code is not working for me
override func layoutSubviews() {
super.layoutSubviews()
let padding = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
bounds = bounds.inset(by: padding)
}
Better approach is the most simple way that make your cells height a bit bigger(3pt from top and 7pt from the bottom) than that of your cell's Total height, and making the colour as [UIColor clearColor]. That would give the illusion that the cells have a 10 pt gap in between.
OR
Add UIView() of 10 height at the end and give it clear color it show space between two cells

Uicollection view dynamic height but fixed width

I am working with UICollectionView I have 4 different kind of cells. Every cell is designed in xib file . when i load them in collection view it goes out of screen. i don't know why it is happening. Some cells have fixed height while some have dynamic height (depends on data coming from API). So is there any possible way to solve this issue.
i have tried Estimate size automatic to dynamic
override func awakeFromNib() {
super.awakeFromNib()
self.contentView.translatesAutoresizingMaskIntoConstraints = false
let screenwidth = UIScreen.main.bounds.size.width
widthAnchor.constraint(equalToConstant: screenwidth-20)
}
func setSize () {
let layout = mainCollection.collectionViewLayout as! UICollectionViewFlowLayout
layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)}
}
try this!
let screenWidth = self.CollecionView.frame.width

Add a button that scrolls with a UITableView in Swift

I am trying to make a UIButton (or tappable UIView) appear above a UITableView. That is easy. The hard part is making it scroll when the user scrolls down the table view.
I have tried using a UIScrollView like so:
private var scroll = UIScrollView();
private var table = UITableView();
override func viewDidLoad()
{
super.viewDidLoad()
scroll = UIScrollView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height));
table = UITableView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height));
table.register(ATableCell.self, forCellReuseIdentifier: "reuseIdentifier");
table.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0);
table.backgroundColor = UIColor.clear;
table.separatorInset = .zero;
table.layoutMargins = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10);
table.rowHeight = UITableViewAutomaticDimension;
// table.estimatedRowHeight = 100;
table.dataSource = self;
table.delegate = self;
scroll.addSubview(table);
self.view.addSubview(table);
}
The scrollbar space at the top can hold a button but will always be there. I want it to stay above the table view but be out of focus when the user scrolls down (like a web page or like the standard mail app in the iphone which does this with the search bar and a table view). Thanks.
You can use the following 2 approaches to add a button above your tableview:
1. Section Header:
Add a section header in your top most section of your tableview. Do not forget to change the UITableViewStyle to plain or else the section header will stick on top.
2. Table Header View:
Add a tableHeaderView and place anything you want here. It will scroll as you scroll the tableview.
Hope it helps.
You can detect scrolling of UITableView using delegate method, where you can set position of UIButton so it can move according to your UITableView scrolling.
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
if (scrollView == myTableView){
// Your code here.....
}
}

How to remove top space from Top of TableView?

In my View controller i have 1 tableview with different Data.
Top Blue colour is View & bottom Blue colour is TabBar.
in Home Screen there's top View & in Trending screen i Hide it by changing it's constraint to 0.
Home and 2. Trending
When i scroll tableview to see new posts & when i click on Trending View i load new data in TableView & scroll tableview to top with the code.
dispatch_async(dispatch_get_main_queue(), ^{
[self.tblVW setContentOffset:CGPointZero animated:NO];
[self.tblVW reloadData];
});
but it will not move to top of tableview.
There's a space at top & when i touch tableview it will scroll to top.
In a grouped tableview,If you want to remove the default header in the top area, try setting:
tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 0.1))
Grouped TableView:
Before:
After:
I guess you are using grouped tableview. Kindly make
self.tblVW.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0,0,0,0.01f)];
Even set height of tableview section header as 0.01f.
YourTableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
Set Above code.
You need to disable automaticallyAdjustsScrollViewInsets .
add this line ViewDidLoad
self.automaticallyAdjustsScrollViewInsets = false
Try this to scroll the table view to top
dispatch_async(dispatch_get_main_queue(), ^{
[self.tblVW.scrollToRowAtIndexPath(NSIndexPath(forRow: 0,inSection: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: true)];
[self.tblVW reloadData];
});
instead of setting content offset.
This works for me, put this code inside the viewDidLoad() tableView.contentInset = UIEdgeInsets(top: -35, left: 0, bottom: 0, right: 0)
Example:
override func viewDidLoad() {
super.viewDidLoad()
self.setupTableView()
}
func setupTableViews() {
tableView.contentInset = UIEdgeInsets(top: -35, left: 0, bottom: 0, right: 0)
}

Button with Image and Text vertically aligned using autolayout constraints

I m very new to IOS development and AutoLayout .
I am facing issues to align the Image and Text inside UIbutton using Storyboard. I had tried to achieve it with TitleEdgeinset and ImageEdge insets accordingly to place the Title ( text ) vertically centered below the Image. But the issue is I have 3 similar buttons which are Vertically stacked ( StackView) and the Text is dynamically set since we have localized strings ( includes Arabic rtl ) .
The image and text moves according to the text length. Is there any ways that I can achieve to make all the buttons with image and text vertically alligned.Also, different screen resolutions are not currently working if using edge insets. Appreciate your help . Thanks in advance.
Few days ago, I solved similar problem,try this
private func adjustImageAndTitleOffsetsForButton (button: UIButton) {
let spacing: CGFloat = 6.0
let imageSize = button.imageView!.frame.size
button.titleEdgeInsets = UIEdgeInsetsMake(0, -imageSize.width, -(imageSize.height + spacing), 0)
let titleSize = button.titleLabel!.frame.size
button.imageEdgeInsets = UIEdgeInsetsMake(-(titleSize.height + spacing), 0, 0, -titleSize.width)
}
call this method for each button, like
self.adjustImageAndTitleOffsetsForButton(yourButton)
Combining Ajay's and Matej's answer:
import Foundation
import UIKit
class VerticalButton: UIButton {
override func awakeFromNib() {
super.awakeFromNib()
self.contentHorizontalAlignment = .left
}
override func layoutSubviews() {
super.layoutSubviews()
centerButtonImageAndTitle()
}
private func centerButtonImageAndTitle() {
let titleSize = self.titleLabel?.frame.size ?? .zero
let imageSize = self.imageView?.frame.size ?? .zero
let spacing: CGFloat = 6.0
self.imageEdgeInsets = UIEdgeInsets(top: -(titleSize.height + spacing),left: 0, bottom: 0, right: -titleSize.width)
self.titleEdgeInsets = UIEdgeInsets(top: 0, left: -imageSize.width, bottom: -(imageSize.height + spacing), right: 0)
}
}
I've modified Ajay's answer because my images weren't centered:
func centerButtonImageAndTitle(button: UIButton) {
let spacing: CGFloat = 5
let titleSize = button.titleLabel!.frame.size
let imageSize = button.imageView!.frame.size
button.titleEdgeInsets = UIEdgeInsets(top: 0, left: -imageSize.width, bottom: -(imageSize.height + spacing), right: 0)
button.imageEdgeInsets = UIEdgeInsets(top: -(titleSize.height + spacing), left: -imageSize.width/2, bottom: 0, right: -titleSize.width)
}
This is a solution that worked for me. Just set the button class to HorizontallyCenteredButton in storyboard and set the title and image top and bottom insets according to your needs (to place the image higher than title) and the button will adjust horizontal insets automatically so that the image is centered above title.
class HorizontallyCenteredButton: LocalizedButton {
override func awakeFromNib() {
super.awakeFromNib()
self.contentHorizontalAlignment = .left
}
override func layoutSubviews() {
super.layoutSubviews()
self.centerButtonImageAndTitle()
}
func centerButtonImageAndTitle() {
let size = self.bounds.size
let titleSize = self.titleLabel!.frame.size
let imageSize = self.imageView!.frame.size
self.imageEdgeInsets = UIEdgeInsets(top: self.imageEdgeInsets.top, left: size.width/2 - imageSize.width/2, bottom: self.imageEdgeInsets.bottom, right: 0)
self.titleEdgeInsets = UIEdgeInsets(top: self.titleEdgeInsets.top, left: -imageSize.width + size.width/2 - titleSize.width/2, bottom: self.titleEdgeInsets.bottom, right: 0)
}
}
To align all the buttons vertically, first select the buttons, then click the button on the bottom-right of the storyboard titled "Align", and finally select "Vertical Centers" in the menu that appears. That should do the trick.
I created a view, then put a VStack with image and text, as well as button.
Make sure the VStack constraints are 0 on all 4 sides so it covers the button :)
this is an easy one, so apologies if you have already tried it!
you don't need to set anything for insets, just make sure that the 3 buttons are aligned with horizontal centres ...
select the controls you want to align, and click on the button highlighted below (bottom right on the xCode screen) and select the Vertical Centres option to align the three with each other, or select Horizontally in Container to put them in the middle of your view.

Resources