iOS8 TabbarController inside a UISplitviewController Master - uitabbarcontroller

I've tried to expand the default Apple MasterDetail Template by adding a UITabbarController in front of the UINavigationController of the MasterView, so there is a structure like this:
UISplitViewController (Master) > UITabbarController > UINavigationController > UITableViewController
But if I run the App, after changing application(didFinishLaunchingWithOptions) to use the correct ViewController, and try to perform the ShowDetails Segue the DetailsView ist presented Modally on the iPhone. On the other side the iPad Version is working as expected.
What am I forgot to do? Or how can I fix it?

I figured out how to put the detail on to the master's UINavigationController instead of presenting it modally over the UITabBarController.
Using the UISplitViewControllerDelegate method
- splitViewController:showDetailViewController:sender:
In case the UISplitViewController is collapsed get the masters navigation controller and push the detail view onto this navigation controller:
- (BOOL)splitViewController:(UISplitViewController *)splitViewController
showDetailViewController:(UIViewController *)vc
sender:(id)sender {
NSLog(#"UISplitViewController collapsed: %d", splitViewController.collapsed);
// TODO: add introspection
if (splitViewController.collapsed) {
UITabBarController *master = (UITabBarController *) splitViewController.viewControllers[0];
UINavigationController *masterNavigationController = (UINavigationController *)master.selectedViewController;
// push detail view on the navigation controller
[masterNavigationController pushViewController:vc animated:YES];
return YES;
}
return NO;
}

Just to update the answers above. Since you can't push navigation controllers anymore, you have to push its top view controller instead.
func splitViewController(splitViewController: UISplitViewController, showDetailViewController vc: UIViewController, sender: AnyObject?) -> Bool {
if splitViewController.collapsed {
let tabBarController = splitViewController.viewControllers.first as! UITabBarController
let selectedNavigationViewController = tabBarController.selectedViewController as! UINavigationController
// Push view controller
var viewControllerToPush = vc
if let navController = vc as? UINavigationController {
viewControllerToPush = navController.topViewController
}
selectedNavigationViewController.pushViewController(viewControllerToPush, animated: true)
return true
}
return false
}

Here's my solution. Place in MasterViewController.m and remember to give your detail view a Storyboard ID in IB. In my case 'detail'.
-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
if ([identifier isEqualToString:#"showDetail"] && self.splitViewController.collapsed) {
DetailViewController *myController = (DetailViewController *)[self.storyboard instantiateViewControllerWithIdentifier:#"detail"];
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
[myController setDetailItem:object];
[self.navigationController showViewController:myController sender:self];
return NO;
}
return YES;
}

There is another way to do it without code.
After you embedded the the UINavigationController in the TabBarController embed the TabBarController in another UINavigationController. So you will have: SplitViewController -> Master -> NavCon -> TabBar -> NavCon -> TableViewController.
It's much easier doing like this, but there a bug that I haven't found out how to fix. The navigation bar presented will be that of the TabBarController, not the TableViewController. Any ideas how to fix that?

Subclass TabBarController like this:
- (void)showViewController:(UIViewController *)vc sender:(id)sender
{
if ([self.selectedViewController isKindOfClass:UINavigationController.class])
[self.selectedViewController showViewController:vc sender:sender];
else
[super showViewController:vc sender:sender];
}
- (UIViewController*)separateSecondaryViewControllerForSplitViewController:(UISplitViewController *)splitViewController
{
return [self.selectedViewController separateSecondaryViewControllerForSplitViewController:splitViewController];
}
- (void)collapseSecondaryViewController:(UIViewController *)secondaryViewController forSplitViewController:(UISplitViewController *)splitViewController
{
[self.selectedViewController.navigationController collapseSecondaryViewController:secondaryViewController forSplitViewController:splitViewController];
}
See this question for complete explanation.

Here is an alternative that is based on testing the size classes of the splitViewController :
Use a custom UISplitViewController (subclass)
Override the showDetailViewController operation
Use the traitCollection to determine the class of the UISplitViewController
If the horizontal class is Compact, get the navigationController to call showViewController
Here is the the code of the custom UISplitViewController :
import UIKit
class CustomSplitViewController: UISplitViewController {
override func showDetailViewController(vc: UIViewController!, sender: AnyObject!) {
if (self.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClass.Compact) {
if let tabBarController = self.viewControllers[0] as? UITabBarController {
if let navigationController = tabBarController.selectedViewController as? UINavigationController {
navigationController.showViewController(vc, sender: sender)
return
}
}
}
super.showDetailViewController(vc, sender: sender)
}
}
Do not forget to the set the custom class in the storyboard.
Tested in the simulator of iPhone 6, iPhone 6+ and iPad Air and worked as expected.

Related

How do I hide default CommandMenu in SwiftUI on macOS?

I'm (attempting) switching over my AppDelegate macOS app to the SwiftUI lifecycle - but can't seem to find out how to handle the CommandMenu. I just want to delete these default menu items (Fie, Edit, View, etc...). In the past, I would just delete them from the Storyboard - but I'm not using a storyboard here. Is there a way to delete these items in SwiftUI?
The items I want to delete:
I know how to add new items via:
.commands {
MyAppMenus()
}
But that just adds them inline with the existing menu items.
swiftUI -- override AppDelegate with your custom:
#main
struct PixieApp: App {
#NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
///........
}
code of appDelegate:
final class AppDelegate: NSObject, NSApplicationDelegate {
func applicationWillUpdate(_ notification: Notification) {
if let menu = NSApplication.shared.mainMenu {
menu.items.removeFirst{ $0.title == "Edit" }
menu.items.removeFirst{ $0.title == "File" }
menu.items.removeFirst{ $0.title == "Window" }
menu.items.removeFirst{ $0.title == "View" }
}
}
}
result:
Until SwiftUI adds more support for adjusting menus, I think you have to worry about SwiftUI reseting the NSApp.mainMenu whenever it updates a window.body. I haven't tried every method for adjusting the mainMenu, but of the methods I tried, the flaw was that SwiftUI seems to have no check for whether it last set NSApp.mainMenu or if something else did.
So however you are managing the menu, update it after SwiftUI has.
Use KVO and watch the NSApp for changes on .mainMenu. Then make your changes with a xib, or reseting the whole thing, or editing SwiftUI's menus.
Example:
#objc
class AppDelegate: NSObject, NSApplicationDelegate {
var token: NSKeyValueObservation?
func applicationDidFinishLaunching(_ notification: Notification) {
// Adjust a menu initially
if let m = NSApp.mainMenu?.item(withTitle: "Edit") {
NSApp.mainMenu?.removeItem(m)
}
// Must refresh after every time SwiftUI re adds
token = NSApp.observe(\.mainMenu, options: .new) { (app, change) in
// Refresh your changes
guard let menu = app.mainMenu?.item(withTitle: "Edit") else { return }
app.mainMenu?.removeItem(menu)
}
}
}
struct MarblesApp: App {
#NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some View {
//...
}
}
This seems to work in Xcode 13.4.1 with Swift 5 targeting macOS 12.3.
Hopefully Apple adds greater control soon. It seems Catalyst has other options. Or you can create a traditional AppKit app and insert the SwiftUI views into it.
You can remove command menu items through the AppDelegate file:
override func buildMenu(with builder: UIMenuBuilder) {
super.buildMenu(with: builder)
builder.remove(menu: .services)
builder.remove(menu: .format)
builder.remove(menu: .toolbar)
}
This thread on the Apple Developer forum might help as well: https://developer.apple.com/forums/thread/649096
CommandGroup(replacing: CommandGroupPlacement.appVisibility, addition: {})

How to setUp the first or Initial ViewController after a splashscreen

I have this scenario:
1) When app starts, the app show the SplashScreen.
2) I need this splashScreen to run a simple logic
I am confused with the following:
Problem:
How to make SplashScreen with logic?
I add a new CocoaTouch file and call it SplashVC.swift
and assign it to the UIViewController in the LaunchScreen.storyboard.
**at LaunchScreen.storyboard:**
I see there is an arrow on the left of the ViewController.
This means it is the first VC to be loaded.
**at Main.storyboard**
I have a MainVC which I made it as the first Page to be loaded.
There are two VC which are the First Page to be loaded.
So, how do I do the the following tasks for LuanchScreen for the below :
conditions:
if there is a value in UserDefaults, go to VC1
if there is no value in UserDefaults, got to VC2
|- viewLogin
SplashScreen.swift ----|
|- viewMain
Update
1) Delete the Default LaunchScreen called: LaunchScreen.storyboard
1.1 ) add interface file in the project
in file dialogBox: select the user interface file of type : Launch Screen
name it : splash (it will create splash.storyboard)
2) Add a CocoaTouch File and Call it SplashVC.swift
3) Assign this SplashVC.swift to UIViewController in Splash.Storyboard
4) Click this UIViewController and goto File Inspector
at Interface Builder Document
untick :Use as LaunchScreen
5) click the project and goto General
Goto App Icons and Launch Images section <br/>
Launch screen file : Splash.storyboard<br/>
6) I have the viewControllers I want to navigate to base on Condition in splashVC.swift:
viewLogin.swift
viewMain.swift
7) Add code in SplashVC.swift
func checkLogin()
{
var mainView: UIStoryboard!
mainView = UIStoryboard(name: "MainStoryboard", bundle: nil)
var viewcontroller : UIViewController
if UserDefaults.standard().bool(forKey: "login"){
viewcontroller = mainView.instantiateViewController(withIdentifier: "viewLogin") as! viewLogin
}else {
viewcontroller = mainView.instantiateViewController(withIdentifier: "viewMain") as! viewMain
}
//--- error : value of type SplashVC has no member 'window'
self.window!.rootViewController = viewcontroller
self.window?.makeKeyAndVisible();
}
}
Error :
//--- error : value of type SplashVC has no member 'window'
self.window!.rootViewController = viewcontroller
self.window?.makeKeyAndVisible();
Please help. Thanks
Thanks
it is very simple just remove the launch screen story board and add your own.
just follow the
step:1 add your own storyboard with single view controller and assign
it to your own splashvc class.
step:2 make your splash storyboard as main interface
now you can do any coding in splash like
class SplashVC: UIViewController {
override func viewDidLoad(){
super.viewDidLoad();
checkLogin()
}
override func viewWillAppear(_ animated: Bool)
{
super.viewWillAppear(animated)
}
func checkLogin()
{
var mainView: UIStoryboard!
mainView = UIStoryboard(name: "MainStoryboard", bundle: nil)
var viewcontroller : UIViewController;
if UserDefaults.standard().bool(forKey: "login"){
viewcontroller = mainView.instantiateInitialViewController()!;
}else {
viewcontroller = mainView.instantiateViewController(withIdentifier: "yourSecondVcIde") as! YourSecondVC
}
self.window!.rootViewController = viewcontroller
self.window?.makeKeyAndVisible();
}
}

iOS UITextView don't localize properly in storyboard

I have a storyboard with some text views on it. When I try to localize the storyboard (creating Storyboard.strings file), all of the UITextViews don't localize at all. UILabels are fine.
My config is as follow:
1 Base Storyboard file with 2 Storyboard.strings files
Storyboard Configuration
Storyboard.strings files are ok, its working with UILabels after all:
Storyboard.strings file
Have you found a solution for this problem?
I'm using Xcode 8.3, Swift 3, iOS 10.
You could just make a IBOutlet in Xcode and set the initial UITextView value in the didSet function.
E.g.
#IBOutlet var textView: UITextView {
didSet {
textView.text = NSLocalizedString("CUSTOM_LOCALISED_STRING", comment: "Comment.")
}
}
I ended up with this solution:
In a UIViewController I can access my UITextView-objects and force the localized text from Storyboard.strings to be used.
func fixTextViewStoryboardLocalization() {
guard storyboard != nil else {
return // No need to fix it if storyboard is not presented
}
let storyboardName = storyboard!.value(forKey: "name") as! String
for case let textView as UITextView in view.subviews {
if let ident = textView.restorationIdentifier {
textView.text = NSLocalizedString("\(ident).text", tableName: storyboardName, bundle: Bundle.main, value: "", comment: "")
}
}
}
Create a custom MyViewController (pick whatever name you want), call this function in viewDidLoad:
class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
fixTextViewStoryboardLocalization()
}
}
Make sure the storyboard view controller is from class MyViewController:
MyViewController custom class in Storyboard
In order the code above to work, I need to set the Restoration ID for each UITextView in my Storyboard:
UITextView Restoration ID
Final step - localize the text view in Storyboard.strings:
/* Class = "UITextView"; text = "Base text"; ObjectID = "MyTextView-Restoration-ID"; */
"MyTextView-Restoration-ID.text" = "Localized text comes here";
This works for all my UITextViews in all my storyboards.

Cosmicmind/Material: How do i implement PageTabBarController without using AppDelegate.swift?

as we know, to implement PageTabBarController, we need to insert these code in AppDelegate.swift:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:
let viewControllers = [MatchDetailViewController(),ListPlayersViewController(),ChatViewController()]
window = UIWindow(frame: Device.bounds)
window!.rootViewController = MatchViewController(viewControllers: viewControllers, selectedIndex: 0)
window!.makeKeyAndVisible()
}
Now, i need to use PageTabBarController when i want to open detail for my match data. My question is, how to implement it without insert those code in AppDelegate.swift because it will open my MatchViewController (extend from PageTabBarController) for the first app launch.
I have tried this code, but it will cause Crash, and it pointed to my AppDelegate.swift
class MatchViewController: PageTabBarController {
var window: UIWindow?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
open override func prepare() {
super.prepare()
let viewControllers = [MatchDetailViewController(),ListPlayersViewController(),ChatViewController()]
//1st try: Crash
window = UIWindow(frame: Device.bounds)
window!.rootViewController = MatchViewController(viewControllers: viewControllers, selectedIndex: 0)
window!.makeKeyAndVisible()
//2nd try: error
self.rootViewController = MatchViewController(viewControllers: viewControllers, selectedIndex: 0)
//3rd try: crash
self.viewControllers = viewControllers
delegate = self
preparePageTabBar()
}
fileprivate func preparePageTabBar() {
pageTabBar.lineColor = Color.blue.base
pageTabBar.dividerColor = Color.blueGrey.lighten5
pageTabBarAlignment = PageTabBarAlignment.top
pageTabBar.lineAlignment = TabBarLineAlignment.bottom
}
}
extension MatchViewController: PageTabBarControllerDelegate {
func pageTabBarController(_ pageTabBarController: PageTabBarController, didTransitionTo viewController: UIViewController) {
}
}
Linked GitHub Question
Hi, yes there is a way. The PageTabBarController is inherited from aUIViewController`, which allows you to add it as a child of any other UIViewController. That said, you just gave me a great idea. I am going to make a new UIViewController that allows you to add as many child UIViewControllers, which will make this super easy to do. I will make this as a Feature Request.
Until the update, please use the suggested method of adding it as a child UIViewController. Are you familiar with how to do that?
First create AppToolbarController (subclass of ToolbarController) or you can use the one in the Material library demo.
And then from your view controller, you can use:
DispatchQueue.main.async {
let tabbarViewController = AppPageTabbarController(viewControllers: [vc1,vc2,vc3], selectedIndex: 0)
self.present(AppToolbarController(rootViewontroller: tabbarViewController))
}

Proper Swipe gesture recognizer iOS

I haven't been able to find a tutorial on how to properly setup a gesture recognizer for iOS.
I need to detect swipe up & down, and the callbacks for them.
Any help, appreciated. Thanks.
You need two recognizers, one for swiping up, and the other for swiping down:
UISwipeGestureRecognizer* swipeUpGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeUpFrom:)];
swipeUpGestureRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
and for the handler:
- (void)handleSwipeUpFrom:(UIGestureRecognizer*)recognizer {
}
Finally, you add it to your view:
[view addGestureRecognizer:swipeUpGestureRecognizer];
The same for the other direction (just change all the Ups to Downs).
This worked for me in Xcode 7.3. , Swift 2.2.
import UIKit
class Viewcontroller: UIViewController
{
override func viewDidLoad()
{
super.viewDidLoad()
createAndAddSwipeGesture()
}
private func createAndAddSwipeGesture()
{
let swipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(Viewcontroller.handleSwipeLeft(_:)))
swipeGesture.direction = UISwipeGestureRecognizerDirection.Left
view.addGestureRecognizer(swipeGesture)
}
#IBAction func handleSwipeLeft(recognizer:UIGestureRecognizer)
{
print(" Handle swipe left...")
}
}