How can I make a storyboard to the left of an initial view storyboard on Apple Watch using SwiftuI? - swift

My Goal:
To make 3 views side by side, where each view takes up all of the Apple Watches viewable area. Something like Apple's workout app when it's on a current workout.
Using the stack overflow answer here I figured out how to use storyboards to make swipable views to the right of the Initial view, but how can I make a swipable view to the left of the initial storyboard view?
When I connect views with ctrl mouse from one view to another, the only segue option I get is next page, which makes a swipable view to the right.

Well, in SwiftUI 2.0 it looks like there is a very easy way to integrate this with a simple TabView and I'll show an example
struct threeViewsInOne{
#State private var selectedPage = 1
var body: some View {
TabView(selection: $selectedPage) {
ViewA.tag(0)
ViewB.tag(1)
ViewC.tag(2)
}
.tabViewStyle(PageTabViewStyle())
}
}

Related

SwiftUI add view to NavigationView

I'm currently working on rewriting a VoIP app from UIKit to SwiftUI. In the UIKit version, we have an active call screen with a button that when tapped, closes the active call screen and lets you interact with the app. Tapping this button also adds a view to the UINavigationController, basically covers most of the navigation view and when tapped, returns the user back to the active call screen.
I'm experimenting with different ideas in SwiftUI, but haven't hit on any solid ideas yet and wanted to see if the community had any ideas for accomplishing this.
Here's a sample of what the UIKit code:
class MainNavController: UINavigationController {
func showActiveCallBar() {
...
let activeCallBarView = ...
...
view.addSubview(activeCallBarView)
}
}
Here's a gif of what happens when we switch back and forth, sorry, couldn't show the whole app. You can see the whole upper portion of the Navigation View gets covered.
Thank you!
In SwiftUI, we would accomplish something like this with an #State variable, which would track wether we want to show the active call bar.
When your state variable changes, your SwiftUI view will be updated and the active call bar will be shown or hidden. It might look something like this.
struct PlayButton: View {
#State private var showActiveCallBar: Bool = false
var body: some View {
if (showActiveCallBar) {
ActiveCallBarView()
}
// ... Other Views
}
}
The SwiftUI documentation of State has more helpful information, and the SwiftUI docs generally are quite helpful in this regard. You could start with the SwiftUI tutorials https://developer.apple.com/tutorials/swiftui

What is the best way of conditionally rendering certain content in Swift

I am building an app which has a dashboard, and have built it in my main storyboard.
Currently, this dashboard renders the same view - no matter whether there is any content to display or not.
As a result, what I want to do is render this screen if there is data that is associated with the user's account which would be shown on screen. However, if there isn't data - I want to display a 'no data found' screen which has an image and a button to add data.
This second 'no data' screen isn't in my main storyboard, and I don't think the right way to do it is to redirect a user depending upon the result of the query.
In React, this is really straightforward - you can use ternary operators to display certain components based upon a condition.
What is the most efficient and effective way for me to do this in Swift? Would I have to programmatically build the screen for this to work as expected?
In iOS, UIViews are the equivalent of Components in React. If you're using storyboard, you have an initial Root View Controller. This is probably where you placed all your views. You have multiple options here if you wanted a dynamic display depending on some data/condition, most of logic here would be handled in code.
Create View A and View B on the same root view controller, and create references to those constraints in the storyboard. In your code, turn hide/show view A and view B depending on your conditions (set the isHidden property). You will also need to turn off and on certain constraints as well.
Create View A and View B from a IBDesignable XIB file. Then, in your code, either add View A or View B to the view hierarchy on the root view controller depending on your conditions.
Create View A on your root view controller. Create View B an other view controller. This means having 2 view controllers in your storyboard. Upon load, check your conditions. If you need View A, do nothing. If you need View B, remove View A from hierarchy and add View B's View Controller as a child view on top of the root view controller like this:
let vcB = storyboard.instantiateViewController(identifier: "ViewControllerB") as! ViewControllerB
let viewB = vcB.view
self.view.addSubview(viewB)
self.addChild(vcB)
vcB.didMove(toParent: self)

Swift: change size of SwiftUI view presented by a UIKit ViewController

I have a project which has been entirely built using UIKit. We are now trying to slowly introduce SwiftUI for any new UI views that we build into the app.
So y current dilemma is that we have a UIViewController which contains a view with a button that needs to present some kind of a pop-up view containing a list of notifications (which I plan to build using List in SwiftUI. For context, here is how the current view looks (built using UIKit). User would then tap on 'Show me...' and be taken to the new SwiftUI view.
So initially I just tried like this:
#objc func showNotifications() {
let controller = UIHostingController(rootView: NotificationContentView())
self.present(controller, animated: true, completion: nil)
}
This obviously presents a modal view which is great, but I want to be able to control the size of this view. The notification screen should be fairly narrow, not virtually full screen.
I wonder how I might go about implementing this.

Swift storyboards: split view controller not showing properly on MacOS

I am trying to implement a split view controller using storyboards & swift on MacOS:
For some reason only the right side shows up. Any ideas ?
It's actually a window size issue. As it is undefined it push the left side out of view. Just place the cursor on the left side and drag to show the left view.
Hope it helps.
UPDATE:
It's actually NOT a window size issue. It looks like that the split view controller push one side out of view by default. Conversely, I was able to view the two panes correctly using the split view component inserted into a view controller.
I'll be checking for information about this issue in the latest WWDC:
https://developer.apple.com/videos/wwdc/2014/?id=212
https://developer.apple.com/videos/wwdc/2014/?id=214
UPDATE 03/11/14:
After minute 07:00 of the first screencast I got what was going on. Take a look at the screenshot below:
Make the Split View Controller as wide as the main window.
Resize the Split View Items.
Click on the items with the right most Split View Item, add Missing Constraints from the Pin pop up menu.
It may take few tries before you get it right. I tested it three times and it did work.
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/ObjC_classic/index.html#//apple_ref/doc/uid/20001093
NSSplitViewDelegate
class SplitViewController: NSSplitViewController{//,NSSplitViewDelegate
Swift storyboards: split view controller not showing properly
It was really problem for me as well. Now I got solved this problem in my code.
1.Create a class extended NSSplitViewController.
2.Set a class created in phase1 as Custom class on your SplitView which you created on StoryBord.
3.call MySplitView.adjustSubviews() inside viewDidLoad().
import Cocoa
class SplitViewController: NSSplitViewController{//,NSSplitViewDelegate
#IBOutlet weak var MySplitView: NSSplitView!
override func viewDidLoad() {
super.viewDidLoad()
MySplitView.adjustSubviews();
}
override func splitView(splitView: NSSplitView, constrainMinCoordinate proposedMinimumPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
return proposedMinimumPosition + 200
}
}

Mimicking iOS notification view

I am trying to mimic iOS 5's notification view (when you swipe down the view appears) for hub with a left swipe in my view. I created a mimic for a single view controller by adding the hubview as subview and changing the origin of view when making swipe. But With this approach I have to add it to every view I implement in my app. I want to add it to UIViewController as a category. I am not sure how to proceed at this point.
If you don't want every viewController to have to handle the menu then I would suggest having a root viewController that acts like a container. It has a subview that displays the content from your various view controllers and it also has the controls for your menu and any other overlay information you may want to provide.
A category won't work in this case but subclassing will. Just ensure that all of your view controllers are sub classes of the view controller with this functionality implemented.