I am new to swift. I have a viewController
Initially the pickerView is hidden but it appears on clicking on textField then it hides again. I want the button hide upon pickeriew selection and then unhide after selection. This is how i am doing it.
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
pickerView.isHidden = false
return false
}
I can hide the backButton like backButton.isHidden = true but it won't show when the selection is done.
Just make visible the back button when picker value is selected and hide the pickerview
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int,
inComponent component: Int)
{
backButton.isHidden = false
pickerView.isHidden = true
}
If you want to to show the picker view during textfield selection, then you need to add picker view as textfield's input view. You can refer this link also https://blog.apoorvmote.com/uipickerview-as-inputview-to-uitextfield-in-swift/
Related
The SwiftUI PhotoPicker is great for creating a button/label to press & then show a Photo Picker when the label is pressed. However, I'd like to invoke a photo picker not after the Picker's label is pressed, but after a conditional test has passed.
For example, if the user clicks on a button that would invoke a Photo Picker, I'd like to first check to see if the record the image will be attached to has been saved. If the record has been saved, I want to launch the picker. If it hasn't been saved, I'll show an alert asking if they want to save or cancel. If they select save, I'll save the record, THEN I'd like to invoke the photo picker automatically.
So can I invoke the Picker programmatically rather than have the user click it? Thanks for advice!
From iOS 16 you can do this by using the photosPicker(isPresented:
struct DemoView: View {
#ObservedObject var viewModel: DemoViewModel
var body: some View {
VStack {
Text("Demo Project")
}
.photosPicker(isPresented: $viewModel.shouldPresentPhotoPicker, selection: $viewModel.selectedPickerItem)
}
}
class DemoViewModel: ObservableObject {
#Published var shouldPresentPhotoPicker = false
#Published var selectedPickerItem: PhotosPickerItem?
func saveTheRecord() {
/// Make an async call, and wait
shouldPresentPhotoPicker = true // Shows the Picker
}
}
I have 2 screens with which I am navigating between them. One screen is for clicking a button to navigate to the other screen and the other screen is for presenting some other information.
Screen one
I have the following piece of swift code since screen 2 uses third part library
let host = UIHostingController(rootView: AnyView(vc))
let scenes = UIApplication.shared.connectedScenes
let windowScene = scenes.first as? UIWindowScene
let application = windowScene?.windows.first
ScanGuideContainer(title: self.viewModel.defaultLocalizer.stringForKey(key: ""), icon: icon,action: {
application?.rootViewController?.present(host, animated: true, completion: {
})
})
.environmentObject(viewModel)
This automatically launches a modal pop which then navigates to Screen2.
Screen 2
This screen has two options either to go back to the modal by pressing the back button or automatically go back once done processing some information which when it does so it returns a new value which then I can use to dismiss the host automatically since its a boolean value.
I can access the second screen viewController via
secondScreen?.viewController
How can i dismiss the modal from first screen when user presses back button of second screen?
What I tried
.onChange(of: self.ViewModel.dismissModal){ newvalue in
if(newvalue) {
host.dismiss(animated: true)
}
}
I set self.ViewModel.dismissModal value from second screen on but it does not work. please help
I am a beginner and I wanted to create a side menu on the right side.
So far I have a UIButton on right top side of viewController, what I want is when I click that button I want to show/hide slide menu with say 3 items…
when I click each item it will go to the different view controller. In my project, i am showing slide menu in only one viewController using AMSlideMenu. Thanks in advance.
Anuj just follow the steps-
Create a SideMenuViewController which is sub class of UIViewController , using storyboard how it will look according to the requirement.
Add this SideMenuViewController and its view as a child view controller in parent view controller by UIButton click.
When you done, remove SideMenuViewController from parent View controller and remove its view from parent view.
Repeat 2 and 3 for all view controllers.
Updated code :
Declare in your view controller -
var sideMenuViewController = SideMenuViewController()
var isMenuOpened:Bool = false
In viewDidLoad
sideMenuViewController = storyboard!.instantiateViewController(withIdentifier: "SideMenuViewController") as! SideMenuViewController
sideMenuViewController.view.frame = UIScreen.main.bounds
In your button Clicked event -
func openAndCloseMenu(){
if(isMenuOpened){
isMenuOpened = false
sideMenuViewController.willMove(toParentViewController: nil)
sideMenuViewController.view.removeFromSuperview()
sideMenuViewController.removeFromParentViewController()
}
else{
isMenuOpened = true
self.addChildViewController(sideMenuViewController)
self.view.addSubview(sideMenuViewController.view)
sideMenuViewController.didMove(toParentViewController: self)
}
}
For Animation:
let transition = CATransition()
let withDuration = 0.5
transition.duration = withDuration
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromLeft
sideMenuViewController.view.layer.add(transition, forKey: kCATransition)
There is no built-in control in iOS for side-menu. However, you could use different open source libraries to achieve your goal.
Have a look at the following libraries:
https://github.com/John-Lluch/SWRevealViewController
The detailed tutorial for this library:
http://www.appcoda.com/ios-programming-sidebar-navigation-menu/
http://www.raywenderlich.com/32054/how-to-create-a-slide-out-navigation-like-facebook-and-path
There is the Main Storyboard, from there three other storyboards with Navigation Controller, each one connected to a 4 view controllers so they use the back button to go back to their respective storyboard. Each Storyboard has a Storyboard reference to return to the Main Storyboard. Problem: each time the secondaries storyboards return to the main storyboard appears a Navigation Bar with
I used the following code in viewDidLoad():
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBarHidden = true
}
and also on viewWillAppear:
override func viewWillAppear(animated: Bool) {
self.navigationController?.navigationBarHidden = true
}
I have a basic application with a UITabBarController which contains 2 UIViewControllers.
The first ViewController has a UIButton in it.
I'm trying to get the tabBar menu hidden and the button focused when the app launches.
I set the tabBar as hidden, and it does not show on launch as expected. Unfortunately, the button doesn't have the focus.
From what I understand from Apple doc, the "focus chain" engine will set focus on the first visible and focusable item in the window hierarchy.
Can anyone help me on this ?
Thank you.
Solution 1.
I think what you can do :
1.Subclass or Customize TabBarController and set
set first launch = true in viewdidload of tabController
> override weak var preferredFocusedView: UIView? { if
> (self.firstLaunch) {
> self.firstLaunch = false;
> return self.selectedViewController!.preferredFocusedView; } else {
> let view = super.preferredFocusedView
> return view; }
Hope it helps.It worked for me though
Your focus engine takes the rootView controller as TabBarController and then asks for preferred focus view which is returned as UITabBarItem we need to unfocus that subclassing the class and returning canBecomeFocus to NO.
if you want to change the focus of the element in the firtsViewController then you can overrirde func preferredFocusView and reutrn the view you want to be focussed else preferredFocusView
Solution 2. Just set the root View controller as the HomeViewController without the TabBarController and embed the TabBarController from the second page. This is because anyways you don't need to use TabBar on the first page why need to take care of its focus.
TabBar is set to hidden if you hide from the AppDelegate or the view which is loaded but it has the focus.