How to set space between tableView Cells? - ios

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

Related

How to get tableView content to scroll behind blur effect in swift iOS

I am trying to figure out how to have my tableView content scroll behind another view while having the table view bottom start point be above the other view. Basically trying to figure out how Apple does the same effect in there iMessage app where the tableView of a conversation start above the text field but scrolls behind it and has a blur effect.
Thanks in advance
Apple's iMessage text field blur
If I understood you correctly then you would want to do this:-
add tableView in whole screen
add a view of any height (lets say 128) with opacity < 100% or just add Visual Effect View of any height.(make sure your tableView is behind Blur view)
flip table vertically (upside down)
flip table cells too
add contentInset to table view equal to height of blur view so that when table loads the content doesn't load behind blur
add scrollIndicatorInsets again equal to height of blur view so your scroll bar doesn't scroll to top(bottom because inverted) of table.
Try yourself:
Design a tableView with cells > 20,
add Visual Effect View, give height = 128
& add this in your viewDidLoad() :
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
tableView.delegate = self
tableView.dataSource = self
tableView.contentInset = UIEdgeInsets(top: 128, left: 0, bottom: 0, right: 0) //top space of content from table = 128
tableView.transform = CGAffineTransform(scaleX: 1, y: -1) //invert table upside down
tableView.scrollIndicatorInsets = UIEdgeInsets(top: 128, left: 0, bottom: 0, right: 0) //top space of scrollBar from table = 128, top space will act as bottom space after inverting in this case
}
Do same to invert table cells:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! Cell
cell.contentView.transform = CGAffineTransform(scaleX: 1, y: -1) //invert cells too
return cell
}
This should achieve the iMessage like behaviour.

UITableViewController's tableView's Content insets

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?

Swift - Adjust size on separator of UITableViewCell

I have at problem with my cells in a UITableView. I have used the Attribute Inspector to adjust the size. Like this:
But the adjustment only applied on the cells with content. How do I make the separator to be the same size for the whole tableview using Swift or the Attribute Inspector?
For your question, you can try this
tableView.tableFooterView = UIView()
Another way you can do like this.
Then,
In cellForRowAtIndexPath
tableView.separatorStyle = .none
let horizontalGap = 15.0 as CGFloat
// As you want to have equal gaping in left & right side, you have to position the view's origin.x to the constant and have to have the width minus the double of that constant.
let seperatorView = UIView(frame: CGRect(x: horizontalGap, y: cell.frame.size.height - 1, width: cell.frame.size.width - horizontalGap * 2.0, height: 1))
seperatorView.backgroundColor = UIColor.red
cell.addSubview(seperatorView)
In Storyboard
Change Separator as None in Attributes Inspector. Drag UIView and place it inside your cell . Give constraints like, Leading 15, Trailing 15, Bottom 0 and height 1. Change background color to red

Add top and bottom padding to UILabel using Swift

How do you add padding to the top and bottom of the content within a UILabel?
I have tried to find the means of doing it via NSAttributedString text but cannot seem to find a way to apply margins to the top and bottom only.
Do this in swift :-
class Label: UILabel {
override func drawTextInRect(rect: CGRect) {
super.drawTextInRect(UIEdgeInsetsInsetRect(rect, UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)))
}
}

Change a UIButton's text (padding) programmatically in Swift

Still learning Swift and don't know Objective-C. I saw that in order to changing a button's text programmatically requires the use of titleEdgeInsets but I am not really sure how to use it.
I would like to change the text in the button (padding) in the bottom and both the left and the right.
Thanks for any responses and/or examples!
Still valid for:
iOS 12/Xcode 10/Swift 4.2/OSX 10.13.2
iOS 9.1/Xcode 7.1/Swift 2.1/OSX 10.10.5:
The method titleEdgeInsets didn't work for me. My button's frame tightly hugs the text (I didn't specify a frame size), and the frame has a red background color. After doing:
myButton.titleEdgeInsets = UIEdgeInsetsMake(10,10,10,10)
the red background shrunk inwards by 10 pixels on each side, which meant that now some of the text was outside the red background. Using negative values had no effect on the original frame size.
I was able to get padding around the text, effectively making the frame bigger, by doing:
myButton.contentEdgeInsets = UIEdgeInsetsMake(5,5,5,5)
iOS 10 and Swift 3 example
Tested and works on IOS 11 and Swift 4.
refineButton.contentEdgeInsets = UIEdgeInsets(top: 5, left: 0, bottom: 0, right: 0)
You can add padding from top, left, right and bottom by doing these lines of code.
button.titleEdgeInsets.left = 10; // add left padding.
button.titleEdgeInsets.right = 10; // add right padding.
button.titleEdgeInsets.top = 10; // add top padding.
button.titleEdgeInsets.bottom = 10; // add bottom padding.
You can also do this in the storyboard:
For iOS 9.1/Xcode 7.1/Swift 2.1
#IBOutlet weak var ratingButton: UIButton!
override func viewDidLoad() {
ratingButton.contentEdgeInsets = UIEdgeInsets(top:15,right:10,bottom:15,left:10)
super.viewDidLoad()
}
Or you can use a custom class if you don't want to define the properties everytime you create a button;
class UIPaddedButton: UIButton {
let padding = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
required init?(coder: NSCoder) {
super.init(coder: coder)
}
override init(frame: CGRect) {
super.init(frame: frame)
}
override func titleRect(forContentRect contentRect: CGRect) -> CGRect {
return contentRect.inset(by: padding)
}
}
And when initializing;
let btn = UIPaddedButton()
For xcode 13.4 and iOS 12
Tested and working
To make space to both sides of a button
ButtonName.contentEdgeInsets = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)

Resources