I'm just starting out with Swift and SwiftUI, so I'm a total beginner. My SwiftUI project came with a LaunchScreen.storyboard which I thought would be nice to use, but I have no idea how. The app loads the initial ContentView.swift, even though I filled the LaunchScreen.storyboard with an image. Is there any place I need to specify the app should load the LaunchScreen first and then go to the ContentView after like, 2 seconds or something?
I suspect it's probably in SceneDelegate.swift, but I don't know.
// Create the SwiftUI view that provides the window contents.
let contentView = ContentView()
// Use a UIHostingController as window root view controller.
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView.environmentObject(model))
self.window = window
window.makeKeyAndVisible()
}
Using XCode 11.2
Thanks in advance for any help!
Edit: It really is a cache issue. I deleted the ~/Library/Developer/Xcode/DerivedData/ModuleCache directory and removed the app from the simulator and test device. Then rebuilt and working! Thanks!
Related
I am trying to create a toolbar for my macCatalyst app in SwiftUI.
On Mac my toolbar should be in the same line as the tripleDotMenu (red, orange, green).
Next to it I want to have my different buttons (see the first image)
I managed removing my titlebar using:
.withHostingWindow { window in
#if targetEnvironment(macCatalyst)
if let windowScene = window?.windowScene as? UIWindowScene {
windowScene.titlebar?.toolbar = nil
windowScene.titlebar?.titleVisibility = .hidden
}
#endif
}
But I can see that the titlebar is just invisible and not gone (image 2). I am not sure how can can get buttons in the top bar...
Developing for macOS it happened automatically..
Can someone help me please? A solution in SwiftUI would be amazing.
I have an iOS/MacCatalyst app, using Swift, not SwiftUI. As soon as I started using Xcode 14, and running on macOS 13 (Ventura), my macCatalyst apps showed the navigation bar menu inline with the 3 dots menu as you say you get for your macOS app. What Xcode and macOS are you using?
There are some bugs with the new inline navigation menu so I've actually reverted back to the previous menu bar style for macCatalyst which has it below the 3 dots section. I did this by inserting
self.navigationController?.navigationBar.preferredBehavioralStyle = .pad
I am trying to programatically change the Apple Watch StatusBar Clock Text Color in my WatchOS only app written in SwiftUI.
I've tried
.preferredColorScheme(.dark)
.preferredColorScheme(.light)
on var body: some View but that does not seem to take effect. Am I missing a step?
I want to embed some SwiftUI in my UIKit-based UI, and unfortunately Apple doesn't provide UIHostingView, only UIHostingController. Can I more or less ignore that controller and just use its view, or do I really need to add it as a child view controller as well? What happens if I don't?
The problem is that finding the parent view controller can be difficult in some contexts. UIView itself doesn't know anything about view controllers, so I'd have to come up with my own way of keeping track of which is the "current" view controller. And I'd rather not do that unless it's actually necessary.
So far in my experiments it's working fine without adding UIHostingController as a child. Device rotation is handled appropriately, and SwiftUI's dark mode override (.colorScheme()) even works through the embedding.
With UIHostingController(rootView:) you just pass in a SwiftUI View.
You can treat it as a UIView by doing:
let myView = UIHostingController(rootView: Text("Hello world!")).view
And then add it as a subview for example:
let parent = UIView()
parent.addSubview(myView)
I can't change the colour of my navigation bar to transparent. All the other view controllers in my project are fine, only this one. This particular view controller has a scroll view and it is presented by Segue.view
I use the following code in viewDidLoad()
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
This is the identity inspector for the view controller
I found the solution. My background image was inside my View. I put it in the wrong place. see below for correct way
I want to include cocos2d scene besides UIKit GUI elements in my app using kobold2d library, so it should take just part of the screen.
I alter cocos2d glview size like this when initializing the first layer:
UIView * glView = [[CCDirector sharedDirector]view];
CGRect rct = CGRectMake(100, 100, 300, 400);
[glView setFrame:rct];
The view is displayed properly until I change orientation, then glview again becomes fullscreen. Is there a way to disable this behaviour?
XCode 4.5.2, iOS 6, Kobold2D 2.04
Calling reshape after setting the frame might help:
UIView * glView = [[CCDirector sharedDirector]view];
CGRect rct = CGRectMake(100, 100, 300, 400);
[glView setFrame:rct];
[glView reshape];
Now after some experiments I kind of found solution, please correct me if I am wrong. At least this worked for me, hope someone else makes use of it either.
CCDirectorIOS is a UIViewController, and it is usually being set as a rootViewController of app window.
Looks like [window rootViewController] is responsible for making it's view fullscreen every time orientation changes, thus forcing all children to layout (I knew that!!! %).
Therefore, to avoid glView shrinking to the whole window now and then, give window another UIViewController as a rootController, not the CCDirector. That another UIViewController should have it's own brand new UIView. This new rootView is what this rootViewController can resize however he wants, leave alone our glView. Now, assign director as a child of rootController and assign glView as a child of a rootView.
Set glView to size You want - and off we go! Now when rootView resizes, glView doesn't. In my case it still had to resize while maintaining free space proportions so i used setAutoresizingMask on it. please gurus tell what You think.
known caveats:
rootController and navController of KKAppDelegate not used except for proper initial orientation
this raises bar of least ios version to 5 because of addChildViewController: method
the code inside app's AppDelegate (suppose ARC is ON):
-(void) initializationComplete
{
#ifdef KK_PLATFORM_IOS
RootViewController * rootvc = [[RootViewController alloc]init];
UIView * rootView = [[UIView alloc]init];
[rootvc setView:rootView];
[rootvc.view setBackgroundColor:[UIColor blueColor]];//to see views in colors
[window setRootViewController:rootvc];
[rootvc addChildViewController:[CCDirector sharedDirector]];
UIView * glview = [[CCDirector sharedDirector]view];
[rootvc.view addSubview:glview];//[[CCDirector sharedDirector]openGLView]];
CGRect glRect = CGRectInset(rootvc.view.bounds, 50, 50);//our glview frame is smaller than rootView frame
[glview setFrame:glRect];
[glview setAutoresizingMask: UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];//so the view size follows window's size even though it's not fullscreen
#endif
}