CollectionView header content under status bar - ios

Here's a collection view constrained to top, left, right, and bottom of the superview with safe area layout guide enabled:
I want my collection view header to be shown under the status bar. I've achieved this for iPhone 4 - 8+ screen dimensions by unchecking Safe Area Layout Guide in the size inspector for the controller's main view, and adding the following code:
collectionView.contentInset = UIEdgeInsets(top: -20, left: 0, bottom: 0, right: 0)
This looks great for non iPhone X view sizes:
However, for the iPhone X, this leads to the following output:
The iPhone X has its own dimensions for the status bar. Adjusting the top inset further does work, but will over-offset the other device sizes. I am wondering if there's an more elegant way to achieve this behaviour.

Found a solution:
collectionView.contentInset.top = -UIApplication.shared.statusBarFrame.height

The previous solutions work, but this might be the easiest one:
collectionView.contentInsetAdjustmentBehavior = .never

This will do
collectionView.contentInsetAdjustmentBehavior = .never

You should use safeAreaInsets for iphone X
if #available(iOS 11.0, *) {
if let top = UIApplication.shared.keyWindow?.safeAreaInsets.top {
collectionView.contentInset = UIEdgeInsets(top: -top, left: 0, bottom: 0, right: 0)
}
} else {
// Fallback on earlier versions
collectionView.contentInset.top = -UIApplication.shared.statusBarFrame.height
}

Add 2 constraints:
1) view - superview
2) view - safeArea

Related

UITableView Cell should not scroll horizontally swift iOS

I have a table view which has card kind of cells as shown in picture.
I have set the content Inset so that I can get the cells with 20px spacing in left,right and top.
tableVw.contentInset = UIEdgeInsets(top: 20, left: 20, bottom: 0, right: 20)
Its displaying as I expected but the problem is, cells can be moved in all direction. But when I move it to right/top, it automatically comes back to original position. But when I move to left it just goes inside and don't scroll back to original position as shown in picture.
I don't want the cells to move at all or I want the cells to come back to centre if its dragged anywhere also. Please help!
Dont provide uiedgeinsets to tableview instead add a view in uitableview cell that cover up the whole cell and add another uiview inside that view and give constraint from top bottom leading trailing equals to 8 or whatever you want then the cell wont move anyways and u tableview cells will look like it has edgeinsets.
you need to set the clipsToBounds property true
tableview.clipsToBounds = true
If you're using AutoLayout, by setting this only should work for you:
In code:
tableView.alwaysBounceVertical = false
or In Interface Builder:
Just find this option and untick "Bounce Vertically" option.
Here's the reference:
If you're not using AutoLayout:
override func viewDidLayoutSubviews() {
// Enable scrolling based on content height
tableView.isScrollEnabled = tableView.contentSize.height > tableView.frame.size.height
}
and also try clipToBounds
tableview.clipsToBounds = true
I achieved what I wanted by below code.
postsTable.contentInset = UIEdgeInsets(top: 20, left: 20, bottom: 0, right: -20)
And in UITableViewCell class :
override var frame: CGRect{
get {
return super.frame
}
set(newFrame){
var frame = newFrame
frame.size.width = kScreenWidth - 40
super.frame = frame
}
}
Now if I drag the cell left or right also its coming back to original position.

Adjust UITableView to allow for keyboard

What is the magical incantation for adjusting a UITableView's height when the keyboard is shown on-screen FOR ALL DEVICES? In the keyboardDidShow notification, I'm doing
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height - view.safeAreaInsets.bottom, right: 0)
tableView.scrollIndicatorInsets = tableView.contentInset
where the bottom constraint of my table view is Align Bottom to: Safe Area.
This works perfectly on all iPhone 5|6|7|8-type devices, but NOT on iPhone X* devices; the adjustment is not of a sufficient amount so as to move the bottom of the table up to meet the top of the keyboard. It's like ~58 pixels short.
Surely there must be some way to get this to work universally, eh? What am I missing?
This happens because iphones except iPhone X have no bottom safe area .
Try removing the view.safeAreaInsets.bottom.

Adjust insets of UISearchController's searchBar

I'm trying out some designs and I'd like to have an equal 25px margin for all my subviews. UISearchController's searchBar has a 16px padding on either sides out of the box. Do I have to write the search bar textfield from scratch, or is there ANY way, undocumented, swizzling, or otherwise, to accomplish offsetting the native bar to my taste?
If you are using a UISearchController you can set the margins of the searchBar like this (assuming searchBar is the UISearchController instance):
let directionalMargins = NSDirectionalEdgeInsets(top: 0, leading: 25, bottom: 0, trailing: 25)
searchController.searchBar.directionalLayoutMargins = directionalMargins
For UISearchBar text position use:
searchBar.searchTextPositionAdjustment = .init(horizontal: 25, vertical: 0)

UICollectionViewCell contentView padding

I want to set padding for contentView from inside of custom UICollectionViewCell so that it is smaller than the cell itself.
I tried doing that by setting anchors for contentView but this view seems to always be equal size of the cell.
This is what I tried.
self.contentView.translatesAutoresizingMaskIntoConstraints = false;
[self.contentView.topAnchor constraintEqualToAnchor:self.topAnchor constant:5].active = true;
[self.contentView.leftAnchor constraintEqualToAnchor:self.leftAnchor constant:5].active = true;
[self.contentView.rightAnchor constraintEqualToAnchor:self.rightAnchor constant:-5].active = true;
[self.contentView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor constant:-5].active = true;
For a quick solution I created another view inside contentView that I can anchor the way shown above but I hope for a cleaner solution.
You might want to try setting edge insets for the contentView, but if you take that approach you should be pinning the subviews to the layout margins guides and not anchors directly.
I come from Swift, but I think you'll understand me:
contentView.layoutMargins = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)
someSubview.topAnchor.constraintEqualToAnchor(contentView.layoutMarginsGuide.topAnchor).isActive = true

How do I best create a button with a left aligned icon and the text centered in the remaining area

I need to have a button that looks like this:
Right now I went for a view with two buttons inside it. But the problem with that is that I can either hook up the two individual buttons to the same outlet, or add a UITapGestureRecognizer to the container view.
I went with the second option because I thought it would be cleaner, but that only works when I disable the buttons (and therefore disable the press-down animation).
Am I on the right track, and should I just find a way to trigger the button's animation when the UITapGestureRecognizer calls my method? Or is there a better way to do this?
You want to use a single UIButton and adjust the imageEdgeInsets & titleEdgeInsets.
For a Swift example:
extension UIButton {
func centerTextAndImage(spacing: CGFloat) {
let insetAmount = spacing / 2
imageEdgeInsets = UIEdgeInsets(top: 0, left: -insetAmount, bottom: 0, right: insetAmount)
titleEdgeInsets = UIEdgeInsets(top: 0, left: insetAmount, bottom: 0, right: -insetAmount)
contentEdgeInsets = UIEdgeInsets(top: 0, left: insetAmount, bottom: 0, right: insetAmount)
}
}
The size inspector tab for UIButton has an Insets section at the top. Play around with the insets values of title insets and image insets to achieve the desired result.
And you don't need to use multiple buttons in a view or tap gesture to achieve this. Just 1 single button would suffice.

Resources