Adding a dismiss button to numberpad swiftUI [closed] - ios

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Although I found a couple tutorials for this online however this is as far as I could figure out.
HStack{
TextField("Placeholder", text: $text)
.keyboardType(.numberPad)
Button(action: {
self.hideKeyboard()
}, label: {
Image(systemName: "keyboard.chevron.compact.down")
})
}
#if canImport(UIKit)
extension View {
func hideKeyboard() {
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
}
}
#endif
Eventhough this works, I would prefer if I could put the button in a toolbar onto of the keypad which I can't figure out how to do.

if your main aim is to dismiss key board you can do the following
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
if you want to create a tool bar and manage your view so that when a textfield is clicked it doesn't occupy the space of textfield there is a very elegant cocoapod for that you just have to install it .its called IQKeyboardManager.
You just have to write one single line in didFinishLaunchingWithOptions method in AppDelegates .
IQKeyboardManager.shared.enable = true
Thats it now everything will be managed by it.

Related

How to use Timer in Swift? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 months ago.
Improve this question
I am confused about the timer in Swift. Can I set the 'selector' to a function in ContentView: View?
I has searched 1 day and all the method to achieve a timer using #objc before function. But when I add #objc before func update(), then Xcode will report an error for:
#objc can only be used with members of classes, #objc protocols, and concrete extensions of classes
I have searched 1 day and all the method to achieve a timer using #objc before function.
for future reference add suffix or tag "swiftui" before any search related to swiftui, so the search engine you are using gives you swiftui related results. It may still have solutions from UIKit or ObjC, but atleast few will give out right result
timer was initially written in Objc way before SwiftUI came into picture, which is why you are getting those results.
Combine came up with an entirely new Timer publisher method, which can be used here with swiftui. Added an example below
Try this instead
struct ContentView: View {
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
#State private var counter = 0
#State private var running = true
func begin() {
}
func pause() {
}
var body: some View {
VStack {
Text("You have used this app")
.font(.largeTitle)
.padding(.bottom)
Text(String(counter) + " s")
.font(.largeTitle)
.foregroundColor(.blue)
.padding(.top)
.onReceive(timer) { _ in
counter += 1
}
}
.padding()
}
}

How to identify iPhone SHAKE in Swift? [duplicate]

This question already has answers here:
Detect shake gesture IOS Swift
(7 answers)
Closed 5 years ago.
Hi I want to identify the iPhone SHAKE when user shakes their phone, either in background mode or in foreground mode of the app.
Please assist me.
Thanks in advance.
Try something like this:
override func motionBegan(_ motion: UIEventSubtype, with event: UIEvent?) {
print("Device was shaken!")
}
The main trick is that you need to have some UIView (not UIViewController) that you want as firstResponder to receive the shake event messages. Here's the code that you can use in any UIView to get shake events:
class ShakingView: UIView {
override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
if event?.subtype == .motionShake {
// Put in code here to handle shake
}
if super.responds(to: #selector(UIResponder.motionEnded(_:with:))) {
super.motionEnded(motion, with: event)
}
}
override var canBecomeFirstResponder: Bool { return true }
You can easily transform any UIView (even system views) into a view that can get the shake event simply by subclassing the view with only these methods (and then selecting this new type instead of the base type in IB, or using it when allocating a view).
In the view controller, you want to set this view to become first responder:
override func viewWillAppear(_ animated: Bool) {
shakeView.becomeFirstResponder()
super.viewWillAppear(animated)
}
override func viewWillDisappear(_ animated: Bool) {
shakeView.resignFirstResponder()
super.viewWillDisappear(animated)
}
Don't forget that if you have other views that become first responder from user actions (like a search bar or text entry field) you'll also need to restore the shaking view first responder status when the other view resigns!
This method works even if you set applicationSupportsShakeToEdit to NO.
For objective c version refer link How do I detect when someone shakes an iPhone?

Make a user access a view only after it has completed an action IOS Swift 3 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
How can I make that an user can only access a View only after he/she has completed an action? For example, only access a view if the user has responded correctly to a question in a previous view in Swift 3 for IOS development
This is a very broad question, but the physics for poets version is you'd want to add some sort of target action method to monitor your textField. One the question is answered, the user can segue.
One approach would be to have a "next" button that's initially disabled and enabled once the question is correctly answered.
In FirstViewController, you could have an IBOutlet to a nextButton and a textField, like so:
#IBOutlet weak var textField: UITextField!
#IBOutlet weak var nextButton: UIButton!
You'd also control+drag a segue to SecondViewController
Then, in viewDidLoad, you could disable the button initially:
nextButton.isEnabled = false
You can't segue when the button is disabled. The moment it's enabled, you can segue by pressing it.
Then, you could have a method you create to monitor your textField for a correct answer:
// this is the method that monitors your textField
func textFieldDidChange(textField: UITextField) {
if textField.text != "42" {
nextButton.isEnabled = false
}
else {
nextButton.isEnabled = true
}
}
In viewDidLoad, you'd add a target action after your disable nextButton. Here's what viewDidLoad looks like:
override func viewDidLoad() {
super.viewDidLoad()
// this sets the button to disabled...
nextButton.isEnabled = false
// this monitors the textField for the correct answer
textField.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: .editingChanged)
}
Here's a repo I created for you.

How to add something on an image in swift [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have an anatomic picture and on it, I want to print a image like a dot or something when the user taps on the first image (the body) to point out where does it hurts.
I've already read something on UITapGestureRecognizer, but I don't really understood how it works.
Try this:
override func viewDidLoad() {
super.viewDidLoad()
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.handleTap))
self.imageView.addGestureRecognizer(gestureRecognizer)
}
#objc func handleTap(tap: UITapGestureRecognizer) {
let circle = UIView()
circle.center = tap.locationInView(imageView)
circle.frame.size = CGSize(width: 30, height: 30)
circle.layer.backgroundColor = UIColor.redColor().CGColor
circle.layer.cornerRadius = 15
imageView.addSubview(circle)
}

Swift, Pass data from popover controller to previous controller [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
i want to fill text View in current controller from data existing in popover controller
with out reload data in current controller
can i do it ??
You can use custom delegate method to fill the value.
class PopupviewController {
weak var delegate: filleTextViewDelegate?
func calldelegate () {
delegate?.fillTextview(filledData)
}
}
protocol filleTextViewDelegate : class {
func fillTextview(filledData: String)
}
class CurrentViewController :filleTextViewDelegate {
func fillTextview(filledData: String) {
textView.text = filledData
}
}

Resources