I am using the jonkykong SideMenu in a Swift 5 xcode project. The following code worked fine before 6.4.8.
import UIKit
import SideMenu
class SideMenuViewController: UITableViewController {
#IBOutlet weak var headerView: UIView!
#IBOutlet weak var driverNameLabel: UILabel!
#IBOutlet weak var vehicleRegoLabel: UILabel!
var menuItems = [Dictionary<String, String>]()
override func viewDidLoad() {
super.viewDidLoad()
// Config options for the Slide in side menu
SideMenuManager.default.leftMenuNavigationController?.presentationStyle = .menuSlideIn
SideMenuManager.default.leftMenuNavigationController?.presentationStyle.onTopShadowOpacity = 1
// hides the 1px bottom border on the nav bar so the header seemless merges to the navbar
self.navigationController?.navigationBar.setValue(true, forKey: "hidesShadow")
... rest of controller code
After upgrading to 6.4.8 the presentationStyle has no effect. The presentationStyle is always the default of viewSlideOut regardless of what I set in the above code.
I am unable to find anything in the README about changes to this in the latest version. Any assistance would be greatly appreciated.
Please set presentationStyle in SideMenuSettings and assign it to SideMenuNavigationController.
func makeSettings() -> SideMenuSettings{
var settings = SideMenuSettings()
settings.allowPushOfSameClassTwice = false
settings.presentationStyle = .menuSlideIn
settings.statusBarEndAlpha = 0
return settings
}
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let sideMenuNavigationController = segue.destination as? SideMenuNavigationController else { return }
sideMenuNavigationController.settings = makeSettings()
}
Related
Coordinator pattern is an old topic with many libraries trying to solve it and I am learning it in simple example app.
My current set up is 3 rootViewControlers: LoadingStateCoordinator, WelcomeCoordinator, TabBarCoordinator but missing connection between UIKit and coordinators. I am trying to implement it with a UINavigationController but the button is not being called. I need a way to connect to back button and a reusable coordinator that I could push to and dellocate accordingly (that is without RxSwift).*Set up Welcome screen as the parent/main navigation and always be able to come back to it.**
So after user selects a form from modal view (vertical flow) presented I show on a push a TabBarCoordinator (horizontal). All viewControllers have empty.storyboard, UIViewController and Coordinator exept the TabBar.Here I only have a coordinator due to the set up of child tab coordinators and the magic needs to happen on a back button tap. Currenly this only being called when user comes from LoadingStateCoordinator. There I need to send the user back to the Welcome screen so they can change the onboarding set up. Here is the first code for LoadingStateCoordinator:
final class LoadingStateCoordinator: NSObject, Coordinator {
*// MARK: - Inputs required*
var childCoordinators: [Coordinator]
var presenter: UINavigationController
private let window: UIWindow
*// MARK: - Initialization*
init(window: UIWindow) {
self.window = window
childCoordinators = []
presenter = UINavigationController()
}
*// MARK: - Coordinator*
func start() {
let controller: LoadingStateViewController = LoadingStateViewController.instantiate()
window.rootViewController = controller
controller.delegate = self
}
}
*// MARK: - LoadingViewControllerDelegate*
extension LoadingStateCoordinator : LoadingViewControllerDelegate {
func performScreenSwitch() {
if UserDefaults.standard.userWasHere == false {
let tabCoordinator: TabBarCoordinator = TabBarCoordinator(window: window, tabBarController: UITabBarController())
window.rootViewController = presenter
addChildCoordinator(tabCoordinator)
tabCoordinator.start()
presenter.pushViewController(tabCoordinator.tabBarController!, animated: true)
} else {
let welcomeCoordinator = WelcomeCoordinator(window: window, presenter: presenter)
window.rootViewController = welcomeCoordinator.presenter
addChildCoordinator(welcomeCoordinator)
welcomeCoordinator.start()
}
}
}
And here is the TabBarCoordinator that need to perform back to Welcome screen action. When I present popToRootfunction it pushes the Welcome screen but all the button there are disbled. I guess to be retain cycle issue. Do I need funadametally another set up? Is there a way to popToRoot(vc) in this set up? What I tryed ended with runtime error "poping to non existing controller".
TabBarCoordinator code that need to perform this:
final class TabBarCoordinator: NSObject, Coordinator {
internal var presenter: UINavigationController
internal var tabBarController: UITabBarController?
internal var childCoordinators: [Coordinator]
var parentCoordinator: LoadingStateCoordinator?
lazy var leftBtn: UIBarButtonItem = {
let button = UIButton(type: .system)
button.setImage(UIImage(systemName: "arrow.turn.up.left"), for: .normal)
button.sizeToFit()
button.addTarget(self,
action: #selector(self.popToRoot(_:)),
for: .touchUpInside)
return UIBarButtonItem(customView: button)
}()
init(window: UIWindow, tabBarController: UITabBarController) {
self.tabBarController = tabBarController
childCoordinators = []
self.presenter = UINavigationController()
}
func start() {
performGetTabBar()
self.presenter.delegate = self
}
private func performGetTabBar() {
let coordinators: [Coordinator] = generateTabCoordinators()
coordinators.forEach({ coordinator in
coordinator.start()
addChildCoordinator(coordinator)
})
let presenters: [UIViewController] = coordinators.map({ coordinator -> UIViewController in
return coordinator.presenter
})
leftBtn.style = .plain
tabBarController?.navigationItem.leftBarButtonItem = leftBtn
tabBarController?.setViewControllers(presenters, animated: false)
selectTab(type: SurfTripCoordinator.self)
}
private func generateTabCoordinators() -> [Coordinator] {
let calculatorCoordinator: CalculatorCoordinator = CalculatorCoordinator(presenter: UINavigationController())
let tripCoordinator: SurfTripCoordinator = SurfTripCoordinator(presenter: UINavigationController())
let sellCoordinator: SavedTripsCoordinator = SavedTripsCoordinator(presenter: UINavigationController())
return [calculatorCoordinator, tripCoordinator, sellCoordinator]
}
*//this is not being called when coming from vertical flow*
#objc func popToRoot(_ sender: UIBarButtonItem) {
let storyboard: UIStoryboard = UIStoryboard(name: Constants.Storyboards.welcomeViewCoordinator, bundle: nil)
let controller: WelcomeViewController = WelcomeViewController.instantiate(from: storyboard)
tabBarController?.navigationController?.pushViewController(controller, animated: true)
}
}
extension TabBarCoordinator: UINavigationControllerDelegate {
func selectTab<T: Coordinator>(type _: T.Type) {
guard let index = childCoordinators.firstIndex(where: { coordinator in
coordinator is T
}) else {
return
}
tabBarController?.selectedIndex = index
}
}
and here is the current WelcomeCoordinator set up
class WelcomeCoordinator: NSObject, Coordinator {
internal var presenter: UINavigationController
var childCoordinators: [Coordinator]
init(window: UIWindow, presenter: UINavigationController) {
self.presenter = presenter
childCoordinators = []
}
func start() {
let storyboard: UIStoryboard = UIStoryboard(name: Constants.Storyboards.welcomeViewCoordinator, bundle: nil)
let controller: WelcomeViewController = WelcomeViewController.instantiate(from: storyboard)
controller.delegate = self
presenter.pushViewController(controller, animated: true)
}
}
extension WelcomeCoordinator : WelcomeViewControllerDelegate {
func performAddLevel() {
let addLevelCoordinator: AddLevelViewCoordinator = AddLevelViewCoordinator(presenter: UINavigationController())
addLevelCoordinator.start()
addChildCoordinator(addLevelCoordinator)
addLevelCoordinator.presenter.modalPresentationStyle = .fullScreen
presenter.present(addLevelCoordinator.presenter, animated: true, completion: nil)
}
}
sorry for the long post I wish there was more reaktive native way to do this...
Ok so I found partlly a solution the back button solution for my case: not using pushViewController or show because it comes with back button. presenter.setViewControllers([tabCoordinator.tabBarController!], animated: true) and there setting the navBar to hidden. I made my own navItem button to navigate to rootVC. Next step to allocate and remove all child tabBar coordinators on back tap recognized.
My code below is causing a run time error. This works If I code a lazy var as a scrollview. But this does not work if I am just trying to add a object to the view or superview. I have connected nothing from the storyboard and do not want to.
var FIRE: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
playSound()
view.addSubview(FIRE)
FIRE.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
FIRE.topAnchor.constraint(equalTo: view.topAnchor, constant: 20).isActive = true
FIRE.widthAnchor.constraint(equalToConstant: 400).isActive = true
FIRE.heightAnchor.constraint(equalToConstant: 80).isActive = true
}
Change
var FIRE: UIImageView!
To
let FIRE = UIImageView()
I've created an NSViewController with corresponding XIB in IB.
FWIW, I have the following window/view hierarchy:
class RxDemoController: NSWindowController {
#IBOutlet weak var statusPaneView: NSView!
#IBOutlet weak var annunciatorPaneView: NSView!
#IBOutlet weak var trigaStatusPaneView: NSView!
#IBOutlet weak var rxTabView: NSView!
override var windowNibName :String? { return "RxDemoController" }
override func windowDidLoad() {
super.windowDidLoad()
let statusPaneViewController = StatusPaneViewController()
statusPaneView.addSubview( statusPaneViewController.view )
let annunciatorPaneViewController = AnnunciatorPaneViewController()
annunciatorPaneView.addSubview( annunciatorPaneViewController.view )
let trigaStatusPaneController = TrigaStatusPaneController()
trigaStatusPaneView.addSubview( trigaStatusPaneController.view )
let rxTabController = RxTabController()
rxTabView.addSubview( rxTabController.view )
}
}
We can ignore the statesPaneViewController, annunciatorPaneViewController, and trigaStatusPaneController as they are all working properly. The rxTabController object is a tab view, defined in RXTabController.swift as follows:
class RxTabController: NSTabViewController {
#IBOutlet weak var rxDisplay1TabView: NSView!
override var nibName :String? { return "RxTabController" }
override func viewDidLoad() {
super.viewDidLoad()
let rxDisplay1ViewController = RxDisplay1()
rxDisplay1TabView.addSubview( rxDisplay1ViewController.view )
}
RxDisplay1.xib currently contains two objects: a bar graph object (custom component: LinearBarGraph_t which has been working fine in other projects and works fine on the view controller for the first tab), the second object is an NSSlider. The NSSlider is giving me problems. Here's the code for RxDisplay1:
class RxDisplay1: NSViewController {
#IBOutlet weak var linearPwrBG: LinearBarGraph_t!
override func viewDidLoad() {
super.viewDidLoad()
linearPwrBG.pctInRange = 50
}
#IBAction func linearPwrSlider(sender: NSSlider) {
}
}
I have right-dragged from the bar graph object to produce the #IBOutlet weak var linearPwrBG: LinearBarGraph_t! outlet; note that in viewDidLoad I set the pctInRange field to 50. This works fine when the app starts up (the bar graph is filled to the 50% mark).
The problem is with the slider. I've right dragged from the slider to the RxDisplay1 class to produce the linearPwrSlider action function. The connection seems to be fine (little filled dot next to the function declaration and right-clicking on the slider shows the action going to this function).
When I run the code, the display (and tab) properly appear. My bar graph shows 50% power. However, the moment I touch the slider with the mouse the system crashes. I get a break in AppDelegate complaining about "Bad Access".
I've used sliders all the time in past projects. However, this is the first time I've used a tabbed controller so I suspect there is something wrong with the way I've connected the tabbed controller to the rest of the system.
my app uses several libraryies. Bassically KYDrawerController. I want my App to use only potraite Orientation but for one ViewControllar i need both landscape and Potrait.
I have search about allmost every solution on the internet. every solution was to subclass the NavigationControllar and Override ShouldAutoRotate method. but non of them worked for me.
and here is the closer View of the entire StoryBoard
the gray color View is the KYDrawerControllar view which is an libry uses to work as NavigationDrawer like Android.
I have created a custom Class for Navigation Controllar and subclass it to the requierd ViewControllar's Navigation Controllar.
here is the Code
class SettingsNavigation: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
NSLog("visibleControllar", self.visibleViewController!.debugDescription)
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func shouldAutorotate() -> Bool {
//return self.visibleViewController!.shouldAutorotate()
return false
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}
override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
return UIInterfaceOrientation.Portrait
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
here is the ViewControllar
class Settings: UIViewController {
// MARK: - Outlets
#IBOutlet var profileImage: UIImageView!
#IBOutlet var profileName: UILabel!
#IBOutlet var profileRegistrationType: UILabel!
#IBOutlet var logOutButton: FUIButton!
#IBOutlet var subscriptionType: UILabel!
#IBOutlet var subscriptionRegistrationType: UILabel!
#IBOutlet var upgradeSubscriptionButton: FUIButton!
override func shouldAutorotate() -> Bool {
/* if (UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft ||
UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeRight ||
UIDevice.currentDevice().orientation == UIDeviceOrientation.Unknown) {
return false;
}
else {
return true;
}*/
return false
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
} }
and i am using StoryBoard segues to present ViewControllars.
please someone help me with this.
Here is my way:
in your appdelegae.swift:
var shouldSupportAllOrientation = false
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
if (shouldSupportAllOrientation == true){
return UIInterfaceOrientationMask.All
}
return UIInterfaceOrientationMask.Portrait
}
in your entry view which enter the all orientation view (change to support all orientation, here I use a button as an example):
#IBAction func next(sender: UIButton) {
let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appdelegate.shouldSupportAllOrientation = true
self.performSegueWithIdentifier("next", sender: self)
}
in your entry view which enter the all orientation view (change the orientation to support only Portrait):
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appdelegate.shouldSupportAllOrientation = false
}
finally, you may find this works on all iPhone device and iPad ipad2 except iPad air iPad pro; you should check the "requires full screen" in your project general info to ensure you all orientation view can enter landscape.
I'm trying use UIImagePickerController in swift but isn't work...
my ViewController:
class ViewController: UIViewController {
#IBOutlet var imag : UIView = nil
#IBAction func capture(sender : UIButton) {
println("Button capture")
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)
{
var imag = UIImagePickerController()
imag.delegate = self
imag.sourceType = UIImagePickerControllerSourceType.Camera;
imag.mediaTypes = kUTTypeImage
imag.allowsEditing = false
self.presentViewController(imag, animated: true, completion: nil)
}
}
}
I have errors in following line of code
imag.delegate = self
(Type'ViewControlles does confoorm to protocol 'UIImagePickerControllerDelegate')
imagePicker.mediaTypes = kUTTypeImage
(use of unresolved identifier kUTTypeImage)
I have read that kUTTypeImage cant use in swift.but don't know, i am using bad this functions. Any help?
Thanks!!
You should also import MobileCoreServices in the controller:
import MobileCoreServices
and then put the type inside square brackets like this:
image.mediaTypes = [kUTTypeImage]
Swift 2.0 and Higher
image.mediaTypes = [kUTTypeImage as String]
Swift 2.0
In Swift 2.0 (Xcode 7), you need to explicitly cast kUTTypeImage (a CFString) to String:
picker.mediaTypes = [kUTTypeImage as String]
And you still need to import Mobile Core Services for this symbol to be defined:
import MobileCoreServices
That said, the default value of mediaTypes is [kUTTypeImage] anyway, so you don't need to set it if that's what you want.
also you should add UINavigationControllerDelegate to the protocols list of the ViewController
and one of the optional delegate functions (if you a planing to get a picture)
This is the working code for your issue:
import UIKit
import MobileCoreServices
class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){
println("i've got an image");
}
#IBAction func capture(sender : UIButton) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){
println("Button capture")
var imag = UIImagePickerController()
imag.delegate = self
imag.sourceType = UIImagePickerControllerSourceType.Camera;
imag.mediaTypes = [kUTTypeImage]
imag.allowsEditing = false
self.presentViewController(imag, animated: true, completion: nil)
}
}
}
From Your Piece of code its very clear that you are making mistakes at two place one is setting delegate and second is setting Media type imag.mediaTypes = kUTTypeImage
First One:If you look into the delegate definition of UIImagePickerController it requires to confirm two protocol UINavigationControllerDelegate and UIImagePickerControllerDelegate so you have to adopt these two protocols in your viewcontroller class like as
class ViewController: UIViewController,UINavigationControllerDelegate, UIImagePickerControllerDelegate
second error:If you look into the definition part of mediaTypes it clearly requires array of media types to passed so do like this
imag.mediaTypes = [kUTTypeImage]
Apart from this, I have written a very descent class for the same task
It is easy to understand and integrate.
Here you go
//Declare property
var imagePicker:ImageVideoPicker?
//Call below line of code properly, it will return an image
self.imagePicker = ImageVideoPicker(frame: self.view.frame, superVC: self) { (capturedImage) -> Void in
if let captureImage = capturedImage{
//you did it.....
}
}
You have to conform to the delegate like this
class ViewController: UIViewController, UIImagePickerControllerDelegate
Per documentation, by default the media types is set to image so you can go ahead and delete that line since you are only setting it to image.
Do not forget to implement the protocol methods which are outlined in the documentation:
documentation
Try this
import UIKit
import AVFoundation
class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
var imagePicker: UIImagePickerController!
#IBOutlet weak var ImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func takeImage(sender: AnyObject) {
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .Camera
presentViewController(imagePicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
imagePicker.dismissViewControllerAnimated(true, completion: nil)
ImageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
}
}
swift 1.2 syntax:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
let image = info[UIImagePickerControllerOriginalImage]
}