Swift 4 changing UIScrollView indicator color - ios

I tried to modify the color of the UIScrollView indicators. In my case is a table view.
The problem is, in swift 3 I used the code
override func scrollViewDidScroll(_ scrollView: UIScrollView){
let verticalIndicator: UIImageView = (scrollView.subviews[(scrollView.subviews.count - 1)] as! UIImageView)
verticalIndicator.backgroundColor = Color.red
}
It's perfect and it work.
Now the same code in Swift 4 not working.
I tried with
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let color = UIColor.red
guard
let verticalIndicator = scrollView.subviews.last as? UIImageView,
verticalIndicator.backgroundColor != color,
verticalIndicator.image?.renderingMode != .alwaysTemplate
else { return }
verticalIndicator.layer.masksToBounds = true
verticalIndicator.layer.cornerRadius = verticalIndicator.frame.width / 2
verticalIndicator.backgroundColor = color
verticalIndicator.image = verticalIndicator.image?.withRenderingMode(.alwaysTemplate)
verticalIndicator.tintColor = .clear
}
but not working. The scroll indicator exist in my view and is visible with the "Debug View Hierarchy" but is like as background transparent.

Related

swift iOS13 searchBar textfield height change

any chances to change UISearchBar's UITextField height?
I tried almost all solutions which I've could found, but all are unsuccessful.
Updated:
I realized that ios13 searchBar doesn't contains UITextField, but contains UISearchBarTextField class
and here is hierarchy
as I understood we have to looking for the new class for iOS13 instead of UITextField
I've tried looping and search TextField
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
for subView in searchBar.subviews {
for subsubView in subView.subviews{
for subsubsubView in subsubView.subviews{
if #available(iOS 13.0, *) {
if let textField = subsubsubView as? UISearchTextField{
var bounds: CGRect
bounds = textField.frame
bounds.size.height = 40 //(set height whatever you want)
textField.bounds = bounds
textField.layoutIfNeeded()
}
} else {
if let textField = subsubView as? UITextField {
var bounds: CGRect
bounds = textField.frame
bounds.size.height = 40 //(set height whatever you want)
textField.bounds = bounds
}
}
}
}
}
}
and debugger goes into condition and found UITextField and changed height, but on device - there no height changes
What I finally did was to set an UIImageView as a container for the UISearchBar.
Didn't work with a simple UIView, I guess it had to do with intrinsic content size?
func showSearchBar() {
self.searchNavigationItem?.setRightBarButton(nil, animated: false)
guard let searchBar = self.searchBar else {
assert(false, "Need a search bar to display")
return
}
self.searchContainer = UIImageView()
self.searchContainer?.isUserInteractionEnabled = true
let image = // Some image with the corresponding size.
self.searchContainer?.image = image
self.searchContainer?.alpha = 0
self.searchContainer?.frame = // Same size as the image
// Here you add the searchContainer into the titleView.
self.searchNavigationItem?.titleView = self.searchContainer
searchBar.translatesAutoresizingMaskIntoConstraints = false
// Create top/bottom/leading/trailing constraints
self.searchContainer?.addSubview(searchBar)
searchBar.sizeToFit()
self.searchContainer?.addConstraints([topConstraint, bottomConstraint, leadingConstraint, trailingConstraint])
self?.searchContainer?.alpha = 1
}

How to add shadow and remove bottom line in NavigationBar globally?

So I'm looking for how can I add shadow to NavigationBar and remove the bottom black line from NavigationBar? Also if I have searchBar in NavigationBar then shadow should go below to search bar. I want to make a change globally without making a change in every viewController.
https://imgur.com/a/8ogGRaf.jpg
So I already archive these changes except add a shadow below to search controller.
For Shadow:
self.navigationController?.navigationBar.isTranslucent = false
self.navigationController?.navigationBar.layer.shadowColor = UIColor.black.cgColor
self.navigationController?.navigationBar.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
self.navigationController?.navigationBar.layer.shadowRadius = 7.0
self.navigationController?.navigationBar.layer.shadowOpacity = 0.2
For Remove a Bottom Bar:
UINavigationBar.appearance().backIndicatorImage = UIImage(named:"normal")
UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage(named:"normal")
This is What issue I'm getting with search Bar
https://imgur.com/a/QQ9yrbE
2.hide bottom line
public extension UINavigationBar {
/// Hide line under navigation bar
public func hideBottomHairline() {
let navigationBarImageView = hairlineImageViewInNavigationBar(self)
navigationBarImageView!.isHidden = true
}
/// Show line under navigation bar
public func showBottomHairline() {
let navigationBarImageView = hairlineImageViewInNavigationBar(self)
navigationBarImageView!.isHidden = false
}
fileprivate func hairlineImageViewInNavigationBar(_ view: UIView) -> UIImageView? {
if view.isKind(of: UIImageView.self) && view.bounds.height <= 1.0 {
return (view as! UIImageView)
}
let subviews = (view.subviews as [UIView])
for subview: UIView in subviews {
if let imageView: UIImageView = hairlineImageViewInNavigationBar(subview) {
return imageView
}
}
return nil
}
}

Nested UICollectionView does not hide NavigationBar on swipe

I have a UICollectionViewController (embedded in a NavigationViewController), which scrolls a UICollectionView horizontally via paging through some sections:
if let flowLayout = collectionView?.collectionViewLayout as? UICollectionViewFlowLayout {
flowLayout.scrollDirection = .horizontal
flowLayout.minimumLineSpacing = 0
}
collectionView?.backgroundColor = .white
collectionView?.register(FeedCell.self, forCellWithReuseIdentifier: cellId)
//collectionView?.contentInset = UIEdgeInsetsMake(MenuBar.height, 0, 0, 0)
//collectionView?.scrollIndicatorInsets = UIEdgeInsetsMake(MenuBar.height, 0, 0, 0)
collectionView?.isPagingEnabled = true
Each section or page contains another UICollectionView (inside the FeedCell) which scrolls vertically through some UICollectionViewCells.
Inside the UICollectionViewController, I set
navigationController?.hidesBarsOnSwipe = true
which was working as long as there was only one UICollectionView. But since the (Top)CollectionView is scrolling horizontally and is containing additional (Sub)CollectionView, that are scrolling vertically, this feature seems not to work any longer.
I would like the NavigationBar to hide when the (Sub)CollectionView is scrolling vertically. Is there any hack to achieve this?
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if let navigationBar = self.navigationController?.navigationBar {
let clampedYOffset = contentOffset.y <= 0 ? 0 : -contentOffset.y
navigationBar.transform = CGAffineTransform(translationX: 0, y: clampedYOffset)
self.additionalSafeAreaInsets.top = clampedYOffset
}
}
This is a solution that I came up with. Basically modify the transform of the NavigationBar to move it out the way when necessary. I also modify the additionalSafeAreaInset, as this will automatically shift all your content up to fill the space left by the navigation bar.
This function will be called as part of the UICollectionViewDelegate protocol.
This was suitable for my purposes - but if you want the navigation bar to appear when the user rapidly scrolls up (like in safari) you will have to add some additional logic.
Hope this helps!
You can try the code like this (Swift 3.0):
extension ViewController: UICollectionViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let isScrollingUp = scrollView.contentOffset.y - lastY > 0
lastY = scrollView.contentOffset.y
self.navigationController?.setNavigationBarHidden(isScrollingUp, animated: true)
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
if !decelerate {
// show navigation bar ?
}
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
// show navigationBar ?
}
}

Parallax effect is slightly choppy when first loaded in UICollectionView in Swift

I am working on implementing a parallax effect for my UICollectionView. The effect thankfully is working. However, when I scroll the collection vertically for the very first time, there is a choppy effect that occurs. This ONLY happens on the first scroll through of the list. After that, the parallax effect is smooth while scrolling.
Below is my relevant code:
MyViewController.swift:
extension FeedViewController: UIScrollViewDelegate{
func scrollViewDidScroll(scrollView: UIScrollView) {
// Scroll navigation bar animation
let scrolledDown = scrollView.contentOffset.y > -self.topLayoutGuide.length
let newAlpha:CGFloat = scrolledDown ? 0.65 : 0.00
UIView.animateWithDuration(0.25) {
self.navBarBackground.alpha = newAlpha
}
// Parallax effect
for cell in collectionView.visibleCells() {
updateParallaxCell(cell)
}
}
func updateParallaxCell(cell: UICollectionViewCell) {
if let myCell = cell as? MyCustomCell {
myCell.updateParallaxEffect(collectionView, mainView: view)
}
}
}
MyCustomCell.swift
override func awakeFromNib() {
super.awakeFromNib()
backgroundImageOriginalOrigin = backgroundImage.frame.origin
}
func updateParallaxEffect(collectionView: UICollectionView, mainView view: UIView) {
let convertedRect = collectionView.convertRect(self.frame, toView: view) //frame of the cell within the coordinate system of the main view
let distanceFromCenter = CGRectGetHeight(self.frame) / 2 - CGRectGetMinY(convertedRect) //y coordinate distance of the cell from the center of the main view
let difference : CGFloat = 50.0 //maximum relative offset of the image within the cell
let move = -difference / 4 + (distanceFromCenter / self.frame.size.height) * difference // calculated relative offset of the image
let y = self.backgroundImageOriginalOrigin.y + move // newly calculated Y coordinate of the image
self.backgroundImage.frame.origin = CGPointMake(self.backgroundImageOriginalOrigin.x, y) // adjusting the image view frame
}
override func layoutSubviews() {
updateParallaxEffect(collectionView, mainView: mainView)
super.layoutSubviews()
}
Can anyone see what it is I'm doing wrong?
Try adding this to your viewDidLoad():
UICollectionView().setContentOffset(CGPointMake(0, 1), animated: true)
UICollectionView().setContentOffset(CGPointMake(0, 0), animated: false)

custom height for swipe delete button swift

I have custom table cell. Each cell have different height. I want to give swipe delete button with cell height.
I have used view in cell. View height is cell height - 16. Top-bottom margin 8.
So please help me to do this.
Use this code in your Custom Cell Swift class
override func layoutSubviews() {
let fltHeight:CGFloat = 46
var subviews: [Any] = self.subviews
let subview: UIView? = subviews[0] as? UIView
if NSClassFromString("UITableViewCellDeleteConfirmationView") != nil {
if (subview?.isKind(of: NSClassFromString("UITableViewCellDeleteConfirmationView")!))! {
let deleteButtonView: UIView? = (subview?.subviews[0])
var buttonFrame: CGRect? = deleteButtonView?.frame
buttonFrame?.origin.x = (deleteButtonView?.frame.origin.x)!
buttonFrame?.origin.y = (deleteButtonView?.frame.origin.y)!
buttonFrame?.size.width = (deleteButtonView?.frame.size.width)!
buttonFrame?.size.height = fltHeight
deleteButtonView?.frame = buttonFrame!
// Placing at the center of the cell.
subview?.frame = CGRect(x: CGFloat((subview?.frame.origin.x)!),
y: CGFloat((subview?.frame.origin.y)! + ((subview?.frame.size.height)!-fltHeight)/2),
width: CGFloat((subview?.frame.size.width)!),
height: CGFloat(fltHeight))
deleteButtonView?.clipsToBounds = true
subview?.clipsToBounds = true
}
}
}

Resources