I am trying to make a popover that will allow users to swipe between images (is there a better approach to this than using a popover?).
Right now to make the popover, I have spend hours googling on simply how to make a rectangle center in the screen. From the internet my code is:
// get a reference to the view controller for the popover
let popController = UIStoryboard(name: "Event", bundle: nil).instantiateViewController(withIdentifier: "carouselPopover")
// set the presentation style
popController.modalPresentationStyle = UIModalPresentationStyle.popover
let width = view.frame.width
popController.preferredContentSize = CGSize(width: width, height: 300)
// set up the popover presentation controller
popController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.up
popController.popoverPresentationController?.delegate = self
popController.popoverPresentationController?.sourceView = self.view
popController.popoverPresentationController?.sourceRect = CGRect(x: view.bounds.midX, y: view.bounds.midY, width: 0, height: 0)
popController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
// present the popover
self.present(popController, animated: true, completion: nil)
However, for the life of me I cannot understand why the popover is not centered
It only loses its center when I set the popController's preferred content size. Any thoughts?
TL:DR
I want to 1) center the popover on the screen, 2) Make the popover 1:1 ratio, and 3) make the width of the popover proportional to the width of the parent screen. How can I do that without 1000's lines of code.
You can create a custom popover class. Then you establish delegate patterns to determine if user swipes.
protocol PopoverDelegate: class {
func imageviewDidSwipe(_ popover: Popover)
}
class Popover: UIView {
weak var delegate: PopoverDelegate?
init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor.white
}
func setupImage(_ image: UIImage) {
let imageView = UIView(frame: CGRect.zero)
imageView.image = image
self.addSubview(imageView)
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
imageView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
imageView.heightAnchor.constraint(equalToConstant: 50).isActive = true // However big you want
imageView.widthAnchor.constraint(equalToConstant: 50).isActive = true // However big you want
}
func showPopover(over view: UIView) {
view.addSubview(self)
translatesAutoResizingMaskIntoConstraints = false
centerXAnchor.contraint(equalTo: view.centerXAnchor).isActive = true
centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
heightAnchor.constraint(equalToConstant: frame.height).isActive = true
widthAnchor.constraint(equalToConstant: frame.width).isActive = true
}
}
To use...
class vC: UIViewController {
func ImageOnClick() {
// change view to where you want popover to show on top of
let popover = Popover(frame: view.frame)
popover.setupImage(image.png)
popover.delegate = self
showPopover(over: view)
}
}
extension vC: PopoverDelegate {
func imageviewDidSwipe(_ popover: Popover) {
// image swiped
}
}
Related
When present a VC, the default style is not cover full screen and with a rounded corner like below gif illustrated.
But I want to control the height of modalPresentation, let's say a 1/4 screen height by default and dynamic changed according to tableView's rows of popped VC. So I implement a custom modalPresentationStyle on the base VC wit below code.
However, I found these issues after:
The popped VC is not rounded corner but with rectangle corner.
I cannot drag to move the popped VC anymore, it is in fixed position.
It would be more better if I could to increment the popped VC's height per its tableView rows count. Not a must item.
#objc func collectButtonTapped(_ sender: Any?) {
let vc = PlayListViewController()
vc.modalPresentationStyle = .custom
vc.transitioningDelegate = self
present(vc, animated: true)
}
func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
return HalfSizePresentationController(presentedViewController: presented, presenting: presentingViewController)
}
class HalfSizePresentationController: UIPresentationController {
override var frameOfPresentedViewInContainerView: CGRect {
guard let bounds = containerView?.bounds else { return .zero }
return CGRect(x: 0, y: bounds.height * 0.75, width: bounds.width, height: bounds.height * 0.75)
}
}
Try to assign cornerRadius to your vc:
#objc func collectButtonTapped(_ sender: Any?) {
let vc = PlayListViewController()
vc.modalPresentationStyle = .custom
vc.transitioningDelegate = self
// assign corner radius
vc.view.layer.cornerRadius = 20
vc.view.clipsToBounds = true
vc.view.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMinYCorner] // this is for corner radius only for top
present(vc, animated: true)
}
for total control of vc presented position, you can use child vc and auto layout, for present child vc (like a modal presentation style) you can use UIView.animate on child vc top constraint.
this is an example of child vc and auto layout:
import UIKit
class YourController: UIViewController {
private lazy var firstChildVc = AiutiFirst()
let myButton: UIButton = {
let b = UIButton(type: .system)
b.layer.cornerRadius = 10
b.clipsToBounds = true
b.backgroundColor = .black
b.setTitleColor(.white, for: .normal)
b.setTitle("Present", for: .normal)
b.addTarget(self, action: #selector(handlePresent), for: .touchUpInside)
b.translatesAutoresizingMaskIntoConstraints = false
return b
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .blue
addChildVC()
}
var up = false
#objc fileprivate func handlePresent() {
print("present")
if up == false {
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut) {
self.menuDown?.isActive = false
self.menuUp?.isActive = true
self.myButton.setTitle("Dismiss", for: .normal)
self.view.layoutIfNeeded()
} completion: { _ in
print("Animation completed")
self.up = true
}
} else {
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut) {
self.menuUp?.isActive = false
self.menuDown?.isActive = true
self.myButton.setTitle("Present", for: .normal)
self.view.layoutIfNeeded()
} completion: { _ in
print("Animation completed")
self.up = false
}
}
}
var menuUp: NSLayoutConstraint?
var menuDown: NSLayoutConstraint?
fileprivate func addChildVC() {
addChild(firstChildVc)
firstChildVc.view.translatesAutoresizingMaskIntoConstraints = false
firstChildVc.view.layer.cornerRadius = 20
firstChildVc.view.clipsToBounds = true
firstChildVc.view.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMinYCorner] // this is for corner radius only for top
view.addSubview(firstChildVc.view)
menuUp = firstChildVc.view.topAnchor.constraint(equalTo: view.centerYAnchor)
menuDown = firstChildVc.view.topAnchor.constraint(equalTo: view.bottomAnchor)
menuDown?.isActive = true
firstChildVc.view.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
firstChildVc.view.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
firstChildVc.view.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
firstChildVc.didMove(toParent: self)
view.addSubview(myButton)
myButton.bottomAnchor.constraint(equalTo: view.centerYAnchor, constant: -40).isActive = true
myButton.widthAnchor.constraint(equalToConstant: 200).isActive = true
myButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
myButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
}
}
And this is the result:
To animate child vc presentation you can use UIView.animate function for top child vc constraint, or Pan gesture do drag it, or whatever you deem necessary and valid to use...
to show it full screen simple set child vc top anchor to top of intere view:
menuUp = firstChildVc.view.topAnchor.constraint(equalTo: view.topAnchor)
I'm super new to iOS app development using Swift and Xcode. Right now, I'm trying to create a base view with a navbar for all my views instead of using a nav controller. When I started in landscape mode, the navbar looks fine, but the height looks off when I switch to portrait mode. When I start in portrait mode, landscape mode looks off where the nav bar is only half of the full length. It seems that when I change to landscape mode, no nav bar is being drawn and it used the old one... In the code below, I have an orientation listener which draws the navbar again, but it doesn't seem to work.
import UIKit
class BaseViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(BaseViewController.rotated), name: UIDevice.orientationDidChangeNotification, object: nil)
}
deinit {
NotificationCenter.default.removeObserver(self, name: UIDevice.orientationDidChangeNotification, object: nil)
}
#objc func rotated() {
if UIDevice.current.orientation.isLandscape {
print("Landscape")
createNavBarLandscape()
} else {
print("Portrait")
createNavBarPortrait()
}
}
func createNavBarLandscape() {
let navBar = UINavigationBar(frame: CGRect(x: 0, y: 50, width: view.frame.size.width * 2, height: 24))
view.addSubview(navBar)
navBar.barTintColor = .white
let navItem = UINavigationItem(title: "logo")
let logo = UIImage(named: "logo.png")
let imageView = UIImageView(image:logo)
navItem.titleView = imageView
navBar.setItems([navItem], animated: false)
}
func createNavBarPortrait() {
let navBar = UINavigationBar(frame: CGRect(x: 0, y: 50, width: view.frame.size.width, height: 44))
view.addSubview(navBar)
navBar.barTintColor = .white
let navItem = UINavigationItem(title: "logo")
let logo = UIImage(named: "logo.png")
let imageView = UIImageView(image:logo)
navItem.titleView = imageView
navBar.setItems([navItem], animated: false)
}
}
Starting in portrait mode:
Portrait mode:
Landscape mode:
Starting in landscape mode:
Landscape mode:
Portrait mode:
Give this a try...
class MyBaseViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let navBar = UINavigationBar()
view.addSubview(navBar)
navBar.barTintColor = .white
let navItem = UINavigationItem(title: "logo")
let imageView = UIImageView()
if let logo = UIImage(named: "logo") {
imageView.image = logo
}
navItem.titleView = imageView
navBar.setItems([navItem], animated: false)
navBar.translatesAutoresizingMaskIntoConstraints = false
// respect safe area
let g = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
navBar.topAnchor.constraint(equalTo: g.topAnchor),
navBar.leadingAnchor.constraint(equalTo: g.leadingAnchor),
navBar.trailingAnchor.constraint(equalTo: g.trailingAnchor),
])
}
}
I have a TabBarController as my rootViewController. The first tab is a tableView with a NavigationBar. While the tableView is performing its fetch request and loading its contents, I present a loading viewController with a loading indicator. The loadingVC doesn't cover the tabBar, which is great, but it does cover the NavBar, which I'd like to avoid. I'd essentially like the loadingVC to be placed in between the NavBar and the TabBar, so having its views frame be bound - its top to the bottom of the NavBar and its bottom to the top of the TabBar. I can't get this functionality to work and I thought I'd be able to find a solution in .modalPresentationStyle but the options there don't cover what I'm describing.
Scene Delegate Code:
window = UIWindow(frame: UIScreen.main.bounds)
window?.windowScene = windowScene
let rootVC = TBController()
window?.rootViewController = rootVC
window?.makeKeyAndVisible()
TabBarController:
class TBController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
tabBar.isTranslucent = false
// Article TVC
let articleVC = UINavigationController(rootViewController: ArticlesTVC())
let articleIcon = UITabBarItem(title: "News", image: UIImage(systemName: "newspaper"), tag: 0)
articleVC.tabBarItem = articleIcon
articleVC.navigationBar.prefersLargeTitles = false
articleVC.navigationBar.isTranslucent = false
articleVC.navigationBar.barTintColor = .systemBackground
articleVC.definesPresentationContext = true
articleVC.view.clipsToBounds = true
}
Loading VC:
class LoadingVC: UIViewController {
var loadingLabel: UILabel = {
let loadingLabel = UILabel()
loadingLabel.text = "Loading articles..."
loadingLabel.textAlignment = .center
loadingLabel.translatesAutoresizingMaskIntoConstraints = false
loadingLabel.font = .systemFont(ofSize: 18, weight: .heavy)
loadingLabel.backgroundColor = .systemBackground
return loadingLabel
}()
var loadingActivityIndicator: UIActivityIndicatorView = {
let indicator = UIActivityIndicatorView()
indicator.style = .large
indicator.color = .white
indicator.startAnimating()
indicator.translatesAutoresizingMaskIntoConstraints = false
indicator.backgroundColor = .systemBackground
return indicator
}()
var blurEffectView: UIVisualEffectView = {
let blurEffect = UIBlurEffect(style: .regular)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.clipsToBounds = true
blurEffectView.alpha = 0.8
blurEffectView.translatesAutoresizingMaskIntoConstraints = false
blurEffectView.backgroundColor = .systemBackground
return blurEffectView
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemBackground
view.addSubview(loadingLabel)
view.addSubview(blurEffectView)
blurEffectView.frame = view.bounds
view.addSubview(loadingActivityIndicator)
loadingActivityIndicator.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
loadingActivityIndicator.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
view.addSubview(loadingLabel)
loadingLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
loadingLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 50).isActive = true
}
}
TableView:
class ArticlesTVC: UITableViewController {
let loadingVC = LoadingVC()
override func viewDidLoad() {
super.viewDidLoad()
loadingVC.modalPresentationStyle = .currentContext
loadingVC.modalTransitionStyle = .crossDissolve
DispatchQueue.main.async {
self.present(self.loadingVC, animated: true)
}
tableView.separatorStyle = .none
tableView.backgroundColor = .systemBackground
}
}
You might be better having it as a view above your tableView and assign true to its isHidden property when your content has loaded. It’s a little messy in the storyboard, although there are ways around that, but in your case you are adding to your view controller’s view programmatically, anyway, so adding your code to a custom view instead and then adding that view over your table view should be no problem. You can then call the custom view’s methods as you please.
I'm adding a button programaticly to TabBarController using this code:
let tabBarHeight = tabBar.layer.bounds.height * 1.2
let win: UIWindow = ((UIApplication.shared.delegate?.window)!)!
let button = Floaty(frame: CGRect(origin: CGPoint(x: 0.0, y: win.frame.size.height),size: CGSize(width: tabBarHeight, height: tabBarHeight)))
button.center = CGPoint(x: win.center.x, y: win.frame.size.height - tabBar.layer.bounds.height)
button.buttonImage = UIImage(named: "icoZ")
let item = FloatyItem()
item.buttonColor = UIColor(named: "ButtonGreen") ?? UIColor.green
item.iconImageView.image = #imageLiteral(resourceName: "percentButton")
item.handler = { item in
let vc = self.storyboard?.instantiateViewController(withIdentifier: "PercentVC") ?? PercentViewController()
DispatchQueue.main.async {
self.present(vc, animated: true, completion: nil)
button.close()
}
}
button.addItem(item: item)
let item2 = FloatyItem()
item2.buttonColor = UIColor(named: "ButtonGreen") ?? UIColor.green
item2.iconImageView.image = #imageLiteral(resourceName: "scanQr")
item2.handler = { item in
let vc = ScannerViewController()
DispatchQueue.main.async {
self.present(vc, animated: true, completion: nil)
button.close()
}
}
button.addItem(item: item2)
self.view.addSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false
let bottom = button.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -15)
let center = button.centerXAnchor.constraint(equalTo: view.centerXAnchor)
let height = button.heightAnchor.constraint(equalToConstant: tabBarHeight)
let width = button.widthAnchor.constraint(equalToConstant: tabBarHeight)
NSLayoutConstraint.activate([bottom,center,height,width])
but when I present a modal view controller and dismiss it the button is moved to the middle of the screen. like this.
the desired is this.
A quick workaround may be to change your modal presentation style to be overFullScreen. The default modal presentation style removes your view from the hierarchy and re-adds it when the modal is dismissed. That causes your views to re-layout.
If that's not acceptable, start with Xcode's view debugger. I'm guessing that the parent view for the button isn't being laid out correctly. If it is being laid out correctly you can try call setNeedsLayout() on the view in viewWillAppear to trigger a recalculation of the layout.
Finally, what you're doing isn't really best practice. You have no guarantee that the tab bar won't be moved above your button the next time the UITabBarController triggers a layout. This honestly should probably be a custom component if you're concerned about long term stability.
I have multiple storyboards in my app. I want to add a view on always on the top just below the navigation bar for some of the controllers. How Can I achieve this?
I already used navigation delegate and add a view in the window but no luck. Steps to show the gray view in the attached image is.
1. On click of a button on that view controller; a gray view should show and remain on the top of the controllers until all the scanning of the device is not done whether the user should go any of the viewControllers.
You can create a UINavigationController subclass and add the view in it.
class NavigationController: UINavigationController {
let customView = UIView()
let iconImgView = UIImageView()
let msgLbl = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
customView.isHidden = true
customView.translatesAutoresizingMaskIntoConstraints = false
customView.backgroundColor = .gray
view.addSubview(customView)
iconImgView.contentMode = .scaleAspectFit
iconImgView.translatesAutoresizingMaskIntoConstraints = false
customView.addSubview(iconImgView)
msgLbl.numberOfLines = 0
msgLbl.lineBreakMode = .byWordWrapping
msgLbl.textColor = .white
msgLbl.translatesAutoresizingMaskIntoConstraints = false
customView.addSubview(msgLbl)
customView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor).isActive = true
customView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
customView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
iconImgView.widthAnchor.constraint(equalToConstant: 40).isActive = true
iconImgView.heightAnchor.constraint(equalToConstant: 40).isActive = true
iconImgView.centerYAnchor.constraint(equalTo: customView.centerYAnchor).isActive = true
iconImgView.leadingAnchor.constraint(equalTo: customView.leadingAnchor, constant: 15).isActive = true
iconImgView.trailingAnchor.constraint(equalTo: msgLbl.leadingAnchor, constant: 15).isActive = true
msgLbl.topAnchor.constraint(equalTo: customView.topAnchor, constant: 10).isActive = true
msgLbl.bottomAnchor.constraint(equalTo: customView.bottomAnchor, constant: 10).isActive = true
msgLbl.trailingAnchor.constraint(equalTo: customView.trailingAnchor, constant: -15).isActive = true
msgLbl.heightAnchor.constraint(greaterThanOrEqualToConstant: 30).isActive = true
}
func showCustomView(message: String, icon: UIImage) {
msgLbl.text = message
iconImgView.image = icon
customView.isHidden = false
}
func hideCustomView() {
customView.isHidden = true
}
}
Embed all your view controllers in this navigation controller. When you want to show/hide the gray view in a view controller use
Show
(self.navigationController as? NavigationController)?.showCustomView(message: "Any Message", icon: UIImage(named: "anyImage")!)
Hide
(self.navigationController as? NavigationController)?.hideCustomView()
When you push another view controller from the same navigation controller the view won't be hidden until you call the hide method
You can simply create a custom UIView with the relevant frame and call addSubview() on the view you want to add it to.
lazy var customView: UIView = {
let customView = UIView(frame: CGRect.init(x: 0, y: self.view.safeAreaInsets.top, width: UIScreen.main.bounds.width, height: 100))
customView.backgroundColor = .gray
return customView
}()
#IBAction func onTapButton(_ sender: UIButton) {
self.view.addSubview(customView)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.customView.removeFromSuperview()
}
To add it below the navigationBar, use y position of frame as self.view.safeAreaInsets.top. With this your customView will always be aligned below the navigationBar.
You can create the view with the height as per your requirement. I've used height = 100.
Give the correct frame and you can add any view as a subView to another view.