Programmatically created UITabBarController - SIGABRT when click to tabBar - uitabbarcontroller

I tried to create UITabBarController programmatically, but I have a SIGABRT when I click to tabBarItem.
Source:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
tabBarController = [[UITabBarController alloc] init];
UIViewController *vc1 = [[UIViewController alloc] init];
UIViewController *vc2 = [[UIViewController alloc] init];
UIView *v1 = [[UIView alloc] init];
UIView *v2 = [[UIView alloc] init];
vc1.title = #"vc1";
vc2.title = #"vc2";
tabBarController.viewControllers = [[NSArray alloc] initWithObjects:vc1, vc2, nil];
self.view = tabBarController.view;
}

Related

SwiftUI go back to RootView in sceneWillEnterForeground SceneDelegate.swift

we try to reset the current View to Root if App will enter Foreground.
How can we do that in SwiftUI?
func sceneWillEnterForeground(_ scene: UIScene) {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let contentView = RootView().environment(\.managedObjectContext, context)
if let windowScene = scene as? UIWindowScene {
let singleOrder = SingleOrder()
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView.environmentObject(singleOrder))
self.window = window
window.makeKeyAndVisible()
}
}
There is no "go back" but the possible approach is to recreate root view controller, by moving "by default" generated content creation into other delegate method as below...
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
self.window = window
}
}
func sceneWillEnterForeground(_ scene: UIScene) {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let contentView = RootView().environment(\.managedObjectContext, context)
let singleOrder = SingleOrder()
window?.rootViewController = UIHostingController(rootView: contentView.environmentObject(singleOrder))
window?.makeKeyAndVisible()
}

displaying google map within a UIView

I am using google maps with iOS.
this is my code:
class ViewController: UIViewController {
#IBOutlet weak var myMapView: GMSMapView!
override func viewDidLoad() {
super.viewDidLoad()
// Create a GMSCameraPosition that tells the map to display the
// coordinate -33.86,151.20 at zoom level 6.
let camera = GMSCameraPosition.camera(withLatitude: +31.75097946, longitude: +35.23694368, zoom: 17.0)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
mapView.isMyLocationEnabled = true
mapView.mapType = .terrain
self.view = mapView
// Creates a marker in the center of the map.
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: +31.75097946, longitude: +35.23694368)
marker.title = "my location"
marker.map = mapView
}
How would i get the map to be attached to myMapView UIView?
Is there a way for the marker title to appear always?
thanks
To place a google map inside a UIView, then you need to drag a UIView object onto your canvas and subclass it as GMSMapView then create an outlet to it in your code - then you can use the following code to initialise it:
#IBOutlet weak var miniview: GMSMapView!
override func viewDidLoad() {
super.viewDidLoad()
let camera = GMSCameraPosition.camera(withLatitude: +31.75097946, longitude: +35.23694368, zoom: 6.0)
miniview.camera = camera
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: +31.75097946, longitude: +35.23694368)
marker.title = "my location"
marker.map = miniview
}
You just have to use the outlet you created.
class ViewController: UIViewController {
#IBOutlet weak var myMapView: GMSMapView!
override func viewDidLoad() {
super.viewDidLoad()
// Create a GMSCameraPosition that tells the map to display the
// coordinate -33.86,151.20 at zoom level 6.
let camera = GMSCameraPosition.camera(withLatitude: +31.75097946, longitude: +35.23694368, zoom: 17.0)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
mapView.isMyLocationEnabled = true
mapView.mapType = .terrain
// CHANGE THIS
self.myMapView = mapView
// Creates a marker in the center of the map.
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: +31.75097946, longitude: +35.23694368)
marker.title = "my location"
marker.map = mapView
}

UITapGestureRecognizer Error

I have been struggling with this issue for a few hours. I can't seem to understand the procedure of using a UITapGestureRecognizer. Any help would be appreciated.
#IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
let textInView = "This is my text."
textView.text = textInView
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tapResponse(_:)))
tapGesture.numberOfTapsRequired = 1
textView.addGestureRecognizer(tapGesture)
func tapResponse(sender: UITapGestureRecognizer) {
var location: CGPoint = sender.location(in: textView)
location.x = textView.textContainerInset.left
location.y = textView.textContainerInset.top
print(location.x)
print(location.y)
}
}
You have a function (tapResponse) inside a function (viewDidLoad).
Put it outside of viewDidLoad. Then reference it like so:
override func viewDidLoad() {
super.viewDidLoad() // Remember to always call super.
let tapGesture = UITapGestureRecognizer(
target: self,
action: #selector(ViewController.tapResponse(sender:)) // Referencing.
)
tapGesture.numberOfTapsRequired = 1
textView.addGestureRecognizer(tapGesture)
}
// tapResponse is now outside of viewDidLoad:
func tapResponse(sender: UITapGestureRecognizer) {
var location: CGPoint = sender.location(in: imageView)
location.x = textView.textContainerInset.left
location.y = textView.textContainerInset.top
print(location.x)
print(location.y)
}
Done:

Swift 3 NavigationController segue back causes wkwebview to move into wrong position

I have a ViewController containing a WKWebView, the view is positioned correctly the first time it loads, but after moving to another view (I'm opening another view by intercepting links from the WebView) and pressing the navigation item (back button) it briefly appears in the right place, then reloads with the top of the webview behind the navigation bar so the top of the page is cut off.
class HomeVC: BaseViewController, WKNavigationDelegate, WKUIDelegate {
var webView: WKWebView?
override func viewDidAppear(_ animated: Bool) {
self.edgesForExtendedLayout = UIRectEdge.top;
super.viewDidLoad()
addSlideMenuButton()
let screenSize: CGRect = UIScreen.main.bounds
let frameRect: CGRect = CGRect(x: 0, y: 100, width: screenSize.width, height: screenSize.height)
let url: NSURL = Bundle.main.url(forResource: "services", withExtension: "html")! as NSURL
let requestObj: NSURLRequest = NSURLRequest(url: url as URL);
self.webView = WKWebView(frame: frameRect)
self.webView?.load(requestObj as URLRequest)
self.webView?.navigationDelegate = self
self.webView?.uiDelegate = self
self.view = self.webView
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationItem.title = ""
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationItem.title = "SELECT A SERVICE"
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
let link: String = (webView.url?.absoluteString)!
print(link)
if(link == "file:///haircut") {
print(link)
self.openViewControllerBasedOnIdentifier("WebVC")
}
}
I've searched around and can't find any similar issues, nor can I see anything obvious in the code.
You have are calling super.viewDidLoad from func viewDidAppear(), what can cause unexpected behaviour. Therefore your UIViewController subclass will never notify its superclass, that the view has been loaded.
override func viewDidAppear(_ animated: Bool) {
// Do not do this before calling super!
self.edgesForExtendedLayout = UIRectEdge.top;
// You are calling the wrong the function for super
// It should be super.viewDidAppear(animated)
super.viewDidLoad()
addSlideMenuButton()
let screenSize: CGRect = UIScreen.main.bounds
let frameRect: CGRect = CGRect(x: 0, y: 100, width: screenSize.width, height: screenSize.height)
let url: NSURL = Bundle.main.url(forResource: "services", withExtension: "html")! as NSURL
let requestObj: NSURLRequest = NSURLRequest(url: url as URL);
self.webView = WKWebView(frame: frameRect)
self.webView?.load(requestObj as URLRequest)
self.webView?.navigationDelegate = self
self.webView?.uiDelegate = self
self.view = self.webView
}

Cocos2d app not showing in full screen in ipad

My cocos2d ipad app loading properly in ipad. But after the default loading screen, it loads not in full screen.
#implementation IntroLayer
+(CCScene *) scene
{
CCScene *scene = [CCScene node];
IntroLayer *layer = [IntroLayer node];
[scene addChild: layer];
return scene;
}
-(id) init
{
if( (self=[super init])) {
CGSize size = [[CCDirector sharedDirector] winSize];
CCSprite *background;
if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone ) {
background = [CCSprite spriteWithFile:#"Default.png"];
background.rotation = 90;
} else {
background = [CCSprite spriteWithFile:#"Default-Landscape~ipad.png"];
}
background.position = ccp(size.width/2, size.height/2);
[self addChild: background];
}
return self;
}
-(void) onEnter
{
[super onEnter];
[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[HelloWorldLayer scene] ]];
}
#end