Skip to content

Instantly share code, notes, and snippets.

@aheze
Created October 6, 2022 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aheze/660b89707789e75e323c4fcc69caec72 to your computer and use it in GitHub Desktop.
Save aheze/660b89707789e75e323c4fcc69caec72 to your computer and use it in GitHub Desktop.
Tip using Popovers
//
// PhotosVC+Tips.swift
// Find-3
//
// Created by A. Zheng (github.com/aheze) on 10/4/22.
// Copyright © 2022 A. Zheng. All rights reserved.
//
import Popovers
import SwiftUI
extension PhotosViewController {
func showSearchTip() {
var attributes = Popover.Attributes()
attributes.sourceFrame = { [weak self] in self?.getSearchBarFrame() ?? .zero }
attributes.position = .absolute(originAnchor: .bottom, popoverAnchor: .top)
attributes.sourceFrameInset.top = 100
attributes.presentation.animation = .linear(duration: 0.3)
attributes.blocksBackgroundTouches = true
attributes.blocksBackgroundTouchesAllowedFrames = { [weak self] in
var frame = self?.getSearchBarFrame() ?? .zero
frame = frame.inset(by: UIEdgeInsets(top: -16, left: -16, bottom: -16, right: -16))
return [frame]
}
attributes.tag = Identifiers.photosSearchTip
attributes.onDismiss = { [weak self] in
guard let self = self else { return }
self.realmModel.photosSearchTip = true
self.navigationItem.hidesSearchBarWhenScrolling = true
}
let popover = Popover(attributes: attributes) {
TipMessageView(title: "Tap to search") { [weak self] in
guard let self = self else { return }
if let popover = self.popover(tagged: Identifiers.photosSearchTip) {
popover.dismiss()
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
self.searchViewController.searchViewModel.searchController.customSearchBar.becomeFirstResponder()
}
}
} background: { [weak self] in
Group {
if let frame = self?.getSearchBarFrame() {
TipBackgroundView(originFrame: frame)
}
}
}
present(popover)
}
func getSearchBarFrame() -> CGRect {
let frame = searchViewController.searchViewModel.customSearchBar.searchTextField.windowFrame()
return frame
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment