I'm in need of a popup that asks for a time input, I figured an alertview would be the way to go but I have no idea where to begin embedding a time picker into an alertview.
Unless there is a better way of doing this?
For anybody else, I adapted the question in the comments to be a time picker like so:
let vc = UIViewController()
vc.preferredContentSize = CGSize(width: 250,height: 300)
let pickerView = UIDatePicker(frame: CGRect(x: 0, y: 0, width: 250, height: 300))
vc.view.addSubview(pickerView)
let editRadiusAlert = UIAlertController(title: "Choose ETA", message: "", preferredStyle: UIAlertControllerStyle.alert)
editRadiusAlert.setValue(vc, forKey: "contentViewController")
editRadiusAlert.addAction(UIAlertAction(title: "Set ETA", style: .default, handler: nil))
editRadiusAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(editRadiusAlert, animated: true)
You can use this component for the alert feel with both date and time picker
https://github.com/palKaran/UIAlertDateTimePicker
Related
I want to swipe right to show another view and swipe left to get back to first view, Without using storyboard
like this example:
Could someone show me how to do that with swift 4
I would assume that if you were to use a swipe gesture, you would do something like:
let swipeGesture = UISwipeGestureRecognizer()
let scrollView = UIScrollView()
swipeGesture.direction = .right
swipeGesture.addTarget(self, action: #selector(scrollToBeginning))
#objc func scrollToBeginning() {
scrollView.setContentOffset(.zero, animated: true)
}
Or using scrollView.scrollRectToVisible(CGRect(x: 0, y: 0, width: 1, height: 1), animated: true)
I've created an actionSheet with 3 options that push to other view controllers and call a number. This option is on a button on a navigation bar on each screen of my app. I'm trying to find a way to reuse the code without literally re-typing it for every screen. I understand there is a way to do this through subclassing a UIButton but I've searched for days and can't find a definitive answer on how to actually code the subclass. My code right now is the following:
class moreButton: UIButton {
#IBAction func displayActionSheet(_ sender: Any) {
let alertController = UIAlertController(title: nil, message: "Get Your Roast On", preferredStyle: .actionSheet)
let followAction = UIAlertAction(title: "Follow Us", style: .default, handler: {(action: UIAlertAction!)->Void in self.performSegue(withIdentifier: "moveSegue", sender: self)
})
let shopAction = UIAlertAction(title: "Visit Shop", style: .default, handler: {(action: UIAlertAction!)->Void in self.performSegue(withIdentifier: "shopSegue", sender: self)
})
let callAction = UIAlertAction(title: "Call Us Now", style: .default) { _ in
let url:URL = URL(string: "tel://number")!
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(followAction)
alertController.addAction(shopAction)
alertController.addAction(callAction)
alertController.addAction(cancel)
self.present(alertController, animated: true, completion: nil)
}
}
I'm getting the error:
value of type 'moreButton' has no member 'performSegue'
in both the followAction and shopAction lines and "value of type 'moreButton' has no member 'present'" on the last line. This code was working when I had created an IBAction directly from the button but now it seems to have errors that I'm not sure how to correct.
You're getting those errors because performSegue and present are UIViewController methods.
Instead of subclassing UIButton, I'd create a new helper class, with a class method to build the UIAlertController for you. The method would accept a UIViewController as its parameter, so that it can build the UIAlertActions correctly.
You'd call this new method from displayActionSheet like:
let alertController = ActionSheetBuilder.build(viewController: self)
present(alert, animated: true, completion: nil)
Adapting your creation code into the new class would come out something like:
final class ActionSheetBuilder {
class func build(viewController: UIViewController) -> UIAlertController {
let alertController = UIAlertController(title: nil, message: "Get Your Roast On", preferredStyle: .actionSheet)
let followAction = UIAlertAction(title: "Follow Us", style: .default, handler: {(action: UIAlertAction!)->Void in viewController.performSegue(withIdentifier: "moveSegue", sender: viewController) })
let shopAction = UIAlertAction(title: "Visit Shop", style: .default, handler: {(action: UIAlertAction!)->Void in viewController.performSegue(withIdentifier: "shopSegue", sender: viewController) })
let callAction = UIAlertAction(title: "Call Us Now", style: .default) { _ in
let url:URL = URL(string: "tel://2845471035")!
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(followAction)
alertController.addAction(shopAction)
alertController.addAction(callAction)
alertController.addAction(cancel)
return alertController
}
}
I want to create an alert with textfield and picker view.
I want that at top show picker view first and then textfield below picker view.
Here is my code
let alert = UIAlertController(title: "Create Meditation", message: "myMsg", preferredStyle: .alert)
alert.view.addSubview(pickerView)
alert.addTextField { (textField) in
textField.text = "Enter Message"
}
let action = UIAlertAction(title: "Send", style: .default) { (action) in
let textField = alert.textFields
print(textField)
}
let action2 = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alert.addAction(action)
alert.addAction(action2)
let height:NSLayoutConstraint = NSLayoutConstraint(item: alert.view, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: self.view.frame.height * 0.50)
alert.view.addConstraint(height);
self.present(alert, animated: true, completion: nil)
You should create a uiviewcontroller and give it this effect.
For example: In the next image, i created a view as modal. it is a uiviewcontroller with a gray view (In your case, in the gray view you should put the uitextfield and uipickerview). Then, i configure storyboard segue as present modally and to give the effect of the modal in the uiviewcontroller put:
self.view.backgroundColor = UIColor.black.withAlphaComponent(0.8)
You can use the following code :
let alertView = UIAlertController(
title: "Select",
message: "\n\n\n\n\n\n\n\n\n",
preferredStyle: .alert)
let pickerView = UIPickerView(frame:
CGRect(x: 0, y: 50, width: 260, height: 162))
pickerView.dataSource = self
pickerView.delegate = self
pickerView.backgroundColor = UIColor.lightGray.withAlphaComponent(0.5)
alertView.view.addSubview(pickerView)
alertView.addTextField(configurationHandler: configurationTextField)
alertView.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler:nil))
alertView.addAction(UIAlertAction(title: "Ok", style: .default, handler:{ (UIAlertAction) in
guard let text = self.textField?.text else {return}
print(text)
}))
present(alertView, animated: true, completion: {
pickerView.frame.size.width = alertView.view.frame.size.width
})
You can't use UIAlertController for that, as it doesn't have an option to add views other than textfields.
So, you have to write your own control, that will mimic UIAlertController.
You can write view controller and present it modally, or create a view and add it to the controller view.
Also, could be good idea to try to find ready component on github.com or cocoacontrols.com and modify it as you need it.
try these Pods hope it helps you
https://github.com/nealyoung/NYAlertViewController
First post and I'll be honest, I'm pretty bad at coding. I'm working with Swift 3 for a school project and I have a question.
I'm creating multiple subviews through a for in loop through an array...
...and each created subview contains one programmatically created label, each with a different word, and a programmatically created UI Tap Gesture attached to each view...
Import UIKit
class ViewController: UIViewController, UIGestureRecongizerDelegate
{
var array = ["Each","One","Has","Its","Own","Name"]
var label = UILabel()
var DynamicView = UIView()
var count = 0
override func viewDidLoad() {
super.viewDidLoad()
createSubView()
//end SuperView
}
func createSubview()
{
for thing in array {
var string = array[count]
count = count + 1
DynamicView = UIView(frame: CGRect(x: 0, y: 0, width: 120, height: 160))
DynamicView.center = CGPoint(x: xV,y: yV)
DynamicView.backgroundColor = UIColor.gray
self.view.addSubview(DynamicView)
label = UILabel(frame: CGRect(x: 25, y: 25, width: 100, height: 21))
label.center = CGPoint(x: 60, y: 13)
label.textAlignment = .center
label.font = UIFont(name: "Arial", size: 13)
label.text = "\(string)"
self.DynamicView.addSubview(label)
let tapGesture = UITapGestureRecognizer(target: self, action:
#selector(ViewController.tapped(recignizer:)))
DynamicView.isUserInteractionEnabled = true
DynamicView.addGestureRecognizer(tapGesture)
}
}
func tapped (recignizer:UITapGestureRecognizer){
print("\(label.text!)")
}
}
... (I'm excluding the code that separates all the views from being stacked on top of each other, so please assume that these views are spaced out, and this list of views is in a scroll view in my actual code, just not cramming that spaghetti code in here)...
...The result of the code is a layout of subViews, each with its own label that is a different word, and a tap gesture for each subView which triggers a function...
...What I want is a way to tap on each subview and find what the text in the label is. As of right now, when you tap on a view, you get what the label.text was in the last View made, so the result of print("label.text!") --> 'Name' , which makes some sense.
Is there any possible way to find the specific labels text even though they are all called "Label"?
I'm not looking to do a collection view or anything of the sort, I need something of this sort of coding to get what I need done.
Thank you to whoever helped this foolish swift student, sorry that my Swift lingo is hot garbage like my code.
Your problem is you are initializing the same DynamicView and label inside the for loop so it will overwrite and it will have last value from the array. To solved the problem you need to create separate UIView and separate UILabel in for loop. Also you need to set userInteraction of label to true because by default its false.
for thing in array {
let dynamicView = UIView(frame: CGRect(x: 0, y: 0, width: 120, height: 160))
dynamicView.center = CGPoint(x: xV,y: yV)
dynamicView.backgroundColor = UIColor.gray
self.view.addSubview(dynamicView)
let label = UILabel(frame: CGRect(x: 25, y: 25, width: 100, height: 21))
label.center = CGPoint(x: 60, y: 13)
label.textAlignment = .center
label.font = UIFont(name: "Arial", size: 13)
label.text = thing
self.dynamicView.addSubview(label)
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.tapped(recignizer:)))
label.isUserInteractionEnabled = true
label.addGestureRecognizer(tapGesture)
}
Now your all view’s origin is CGPoint.zero so only last view label will visible and when you tap on label to get text of its you can get this way.
func tapped(recignizer:UITapGestureRecognizer){
if let tappedLabel = recignizer.view as? UILabel {
print("\(tappedLabel.text)”)
}
}
I would like to add Image in UIAlertbox so I add the following codes.
let alertController = UIAlertController(title: "Gender", message: "" , preferredStyle: .alert)
// Create the actions
let okAction = UIAlertAction(title: "Female", style: UIAlertActionStyle.default) {
UIAlertAction in
// exit(0)
debugPrint("Press OK")
}
let cancelAction = UIAlertAction(title: "Male", style: UIAlertActionStyle.cancel) {
UIAlertAction in
}
// Add the actions
okAction.setValue(#imageLiteral(resourceName: "female_image"), forKey: "image")
cancelAction.setValue(#imageLiteral(resourceName: "male_image"), forKey: "image")
alertController.addAction(okAction)
alertController.addAction(cancelAction)
When I run the app, only one Image appear. What's wrong with this?
Please anyone help me?
Try this code its working in swift3
let alertMessage = UIAlertController(title: "Gender", message: "", preferredStyle: .alert)
let image = UIImage(named: "blanckstar")
let action = UIAlertAction(title: "Male", style: .default)
{
UIAlertAction in
// exit(0)
debugPrint("Press Male")
}
action.setValue(image, forKey: "image")
let image2 = UIImage(named: "blanckstar")
let action2 = UIAlertAction(title: "Female", style: .default)
{
UIAlertAction in
// exit(0)
debugPrint("Press Female")
}
action2.setValue(image2, forKey: "image")
alertMessage.addAction(action)
alertMessage.addAction(action2)
self.present(alertMessage, animated: true, completion: nil)
Happy Coading :-)
for changing size of image you can try with fontAwesome just install pod
pod 'FontAwesome.swift' and import FontAwesome_swift
here is the code.....
let alertMessage = UIAlertController(title: "Gender", message: "", preferredStyle: .alert)
let image = UIImage.fontAwesomeIcon(name: .male, textColor: UIColor.black, size: CGSize(width: 50, height: 50))
let action = UIAlertAction(title: "Male", style: .default)
{
UIAlertAction in
// exit(0)
debugPrint("Press Male")
}
action.setValue(image, forKey: "image")
let image2 = UIImage.fontAwesomeIcon(name: .female, textColor: UIColor.black, size: CGSize(width: 50, height: 50))
let action2 = UIAlertAction(title: "Female", style: .default)
{
UIAlertAction in
// exit(0)
debugPrint("Press Female")
}
action2.setValue(image2, forKey: "image")
alertMessage.addAction(action)
alertMessage.addAction(action2)
self.present(alertMessage, animated: true, completion: nil)