swift: ios13 status bar setting error occurs in SideMenu library - swift

I introduced SideMenu in CocoaPods, but an error occurs in the ios13 part of the status bar.But I don't know the correct code.
Would you please teach me?
var statusBarFrame: CGRect {
if #available(iOS 13.0, *) {
// How to do for iOS 13??
} else {
return UIApplication.shared.statusBarFrame
}
}

statusBarFrame has been deprecated in iOS 13,
for iOS 13 you can use this
var statusBarFrame: CGRect {
if #available(iOS 13.0, *) {
return UIApplication.shared.keyWindow!.windowScene!.statusBarManager!.statusBarFrame
} else {
return UIApplication.shared.statusBarFrame
}
}

Related

iOS 16 with broken UIDatepicker .inline

Any one notice that app with date picker with style .inline freeze and crash ?
I used below code at my existing app at Appstore
if #available(iOS 14, *) {
datePicker.preferredDatePickerStyle = .inline
}else {
datePicker.preferredDatePickerStyle = .automatic
}
For working .inline with iOS16 I need to force add this condition
if #available(iOS 16, *) {
datePicker.preferredDatePickerStyle = .inline
}else {
datePicker.preferredDatePickerStyle = .inline
}
Having result:

How to enable Dark theme for iOS 12 and lower

I have already added support for iOS13 where there are two switches: first one for the automatic, system based theme and the other switch to turn on and off dark theme and am switching it with the given method:
func changeThemeTo(theme: String){
switch theme {
case "dark":
UIApplication.shared.windows.forEach { window in
if #available(iOS 13.0, *) {
window.overrideUserInterfaceStyle = .dark
} else {
// Fallback on earlier versions
}
}
case "light":
UIApplication.shared.windows.forEach { window in
if #available(iOS 13.0, *) {
window.overrideUserInterfaceStyle = .light
} else {
// Fallback on earlier versions
}
}
case "auto":
UIApplication.shared.windows.forEach { window in
if #available(iOS 13.0, *) {
window.overrideUserInterfaceStyle = .unspecified
} else {
// Fallback on earlier versions
}
}
default: break
}
}
And everything is working fine for iOS 13, but now I need to enable this to work for iOS lower than 13. Is there something I can do similar to window.overrideUserInterfaceStyle = .dark or should I completely change the switching logic as did on https://www.onswiftwings.com/posts/dark-mode/

Compiling for iOS 10.3, but module 'SwiftUICharts' has a minimum deployment target of iOS 13.0

Any idea how I could import SwiftUICharts framework only if the iOS version is over iOS 13?
I added this framework via File > Swift Package > add package dependency.
My app target has to be iOS 10.
I import this framework into a swiftui controller that will only show if the target is over iOS 14, so it's ok to use it there. But I don't know how. A simple #available doesn't work in this case.
EDIT. Here is my code:
AnalyticsNew controller:
#if !arch(arm)
import SwiftUI
#available(iOS 14, *)
struct AnalyticsNew: View {
var body: some View {
Home()
}
}
#available(iOS 14, *)
struct AnalyticsNew_Previews: PreviewProvider {
static var previews: some View {
AnalyticsNew()
}
}
#available(iOS 14, *)
struct Home : View {
//... some code
}
#endif
AnalyticsView controller that shoes the right controller depending on the iOS version:
#if canImport(SwiftUI)
import SwiftUI
#endif
import UIKit
class AnalyticsView: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let child = createViewController()
addChild(child)
child.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
child.view.frame = view.bounds
view.addSubview(child.view)
child.didMove(toParent: self)
}
func createViewController() -> UIViewController {
if #available(iOS 14, *) {
#if !arch(arm) //compatible SwiftUI IOS <10
return UIHostingController(rootView: AnalyticsNew())
#else
return AnalyticsOld()
#endif
} else {
return AnalyticsOld()
}
}
}

How can I turn the MKMapView for dark mode?

How can I change the map to dark mode from iOS 13?
I have opt-out from UserInterfaceStyle so system-wide colors will not apply to me, so I'll do it manually.
I've seen this video from apple WWDC2019 - Session 236, at 8:19s but that's for snapshots and I didn't get it.
Actually I was trying something like:
private var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
mapView.backgroundColor = .black
}
but it doesn't change the theme or appearance or the traitCollection to dark.
Any suggestion?
This is what you need on the viewDidLoad
if #available(iOS 13.0, *) {
self.overrideUserInterfaceStyle = .dark
}
Setting overrideUserInterfaceStyle on viewDidLoad method will force that view controller to use the desired mode you specify.
if #available(iOS 13.0, *) {
overrideUserInterfaceStyle = .light
}
MapSnapShot
if you are trying to take a snapshot and confused with output image colour Use
if #available(iOS 13.0, *) {
mapSnapshotOptions.traitCollection = traitCollection
}
Full code for a MKMapSnapshotter
func mapCamera(location : CLLocationCoordinate2D )-> MKMapSnapshotter {
let mapSnapshotOptions = MKMapSnapshotter.Options()
let region = MKCoordinateRegion(center: location, latitudinalMeters: 500, longitudinalMeters: 500)
mapSnapshotOptions.region = region
mapSnapshotOptions.scale = UIScreen.main.scale
mapSnapshotOptions.size = AppConstants.Size.mapDetailView
mapSnapshotOptions.showsBuildings = true
mapSnapshotOptions.showsPointsOfInterest = true
if #available(iOS 13.0, *) {
mapSnapshotOptions.traitCollection = traitCollection
}
return MKMapSnapshotter(options: mapSnapshotOptions)
}
In case you got here searching for a macOS solution (like me), simply update the appearance property on MKMapView:
if #available(OSX 10.14, *) {
mapView.appearance = NSAppearance(named: .darkAqua) // .aqua for default mode
}

UIscrollview issue with iPhone X

I am having a horizontal UIScrollview in my xib file. Scrollview looks great in all devices except iPhone X, XS and XR.
It looks good in iPhone 8
iPhone X
I have tried all the possible solutions, Unchecked the under top bar, under bottom bar, auto resize subviews,nothing works for me. iPhone X always looks the same. Top constraint of the scrollview is set to safe area. I am using xib file here.
if #available(iOS 11.0, *) {
scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentBehavior.never
} else {
self.automaticallyAdjustsScrollViewInsets = false
edgesForExtendedLayout = []
// Fallback on earlier versions
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.scrollView.contentSize = CGSize(width: self.lectureTable.frame.size.width , height: self.scrollView.frame.size.height)
self.automaticallyAdjustsScrollViewInsets = false
}
Please shed some light. Thanks a ton.
I have solved it by myself. Inside my scroll view, I have used UIView.
var hasTopNotch: Bool {
if #available(iOS 11.0, *) {
return UIApplication.shared.delegate?.window??.safeAreaInsets.top ?? 0 > 20
}
return false
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
var topbarHeight: CGFloat {
return UIApplication.shared.statusBarFrame.size.height + (self.navigationController?.navigationBar.frame.height ?? 0.0)
}
if hasTopNotch{
let window = UIApplication.shared.keyWindow
if #available(iOS 11.0, *) {
let topPadding = window?.safeAreaInsets.top
self.customView.frame.size.height = UIScreen.main.bounds.height - (topbarHeight + topPadding!)
} else {
// Fallback on earlier versions
}
}else{
let NavBarHeight = self.navigationController!.navigationBar.frame.height + UIApplication.shared.statusBarFrame.height
self.customView.frame.size.height = UIScreen.main.bounds.height - NavBarHeight
}
}
This works great in all the devices.