Swift- shifting text field issue - swift

I have 2 text fields both placed on top of a image view. The bottom text field is supposed to shift up when the keyboard appears. When the image view is empty the bottom text field shifts up as expected, but when image is present in image view, the bottom text field doesn't shift up. when executing can see with help of print statements that keyboardWillShow function is not executing. can anyone help here?
Following is my code
class ViewController: UIViewController, UITextFieldDelegate,UINavigationControllerDelegate, UIImagePickerControllerDelegate {
#IBOutlet weak var actualImage: UIImageView!
#IBOutlet weak var shareButton: UIButton!
#IBOutlet weak var deleteButton: UIButton!
#IBOutlet weak var topTextField: UITextField!
#IBOutlet weak var bottomTextField: UITextField!
var activeTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
topTextField.delegate = self
bottomTextField.delegate = self
//Animations
topTextField.isHidden = true
bottomTextField.isHidden = true
shareButton.isEnabled = false
deleteButton.isEnabled = false
let center: NotificationCenter = NotificationCenter.default
center.addObserver(self, selector: #selector(keyboardDidShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
center.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
//Editing text Fields
func textFieldDidBeginEditing(_ textField: UITextField) {
activeTextField = textField
}
#objc func keyboardDidShow(notification: Notification) {
print("keyboarddidshow")
if activeTextField != nil {
let info: NSDictionary = notification.userInfo! as NSDictionary
let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
let keyboardY = self.view.frame.size.height - keyboardSize.height
let textFieldY: CGFloat! = self.activeTextField.frame.origin.y
if self.view.frame.origin.y >= 0{
if textFieldY > (keyboardY - 80){
UIView.animate(withDuration: 0.25,delay:0.0,options:UIViewAnimationOptions.curveEaseIn, animations: {
self.view.frame = CGRect(x: 0, y: self.view.frame.origin.y - (textFieldY - (keyboardY - 80)), width: self.view.bounds.width, height: self.view.bounds.height)
}, completion: nil)
}
}
}
}
#objc func keyboardWillHide(notification: Notification){
print("switch field keyboard will hide")
UIView.animate(withDuration: 0.25,delay:0.0,options:UIViewAnimationOptions.curveEaseIn, animations: {
self.view.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)}, completion: nil
)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
override func viewDidDisappear(_ animated: Bool) {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
//share button pressed
#IBAction func sharePressed(_ sender: UIButton) {
topTextField.borderStyle = .none
topTextField.borderStyle = .none
let image: UIImage = generateMemedImage()
let shareImage = UIActivityViewController(activityItems: [image, topTextField,bottomTextField], applicationActivities: nil)
present(shareImage, animated: true, completion: nil)
}
//allow selecting image from photo library
#IBAction func selectFromGallery(_ sender: Any) {
let gallery = UIImagePickerController()
gallery.delegate = self
gallery.sourceType = .photoLibrary
present(gallery, animated: true, completion: nil)
}
#IBAction func selectFromCamera(_ sender: Any) {
let gallery = UIImagePickerController()
gallery.delegate = self
if UIImagePickerController.isSourceTypeAvailable(.camera){
gallery.sourceType = .camera
present(gallery, animated: true, completion: nil)
} else {
displayAlert(title: "Camera not available", message: "")
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
actualImage.image = selectedImage
dismiss(animated: true, completion: nil)
topTextField.isHidden = false
bottomTextField.isHidden = false
shareButton.isEnabled = true
deleteButton.isEnabled = true
topTextField.text = " "
bottomTextField.text = " "
}
#IBAction func deletePressed(_ sender: Any) {
actualImage.image = nil
topTextField.isHidden = true
bottomTextField.isHidden = true
shareButton.isEnabled = false
deleteButton.isEnabled = false
topTextField.text = " "
bottomTextField.text = " "
}
//display alert
func displayAlert(title: String, message:String){
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Dismiss", style: .default, handler: nil))
present(alert, animated: true, completion: nil)
}
func generateMemedImage() -> UIImage {
// Render view to an image
UIGraphicsBeginImageContextWithOptions(CGSize(width: 375, height: 517), false, 0)
view.drawHierarchy(in: CGRect(x: 0, y: -75, width: view.bounds.size.width, height: view.bounds.size.height), afterScreenUpdates: true)
let memedImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return memedImage
}
}

When you show the camera or the gallery, viewDidDisappear is called, which removes your subscription to the notifications. Perhaps you should subscribe to the notifications in viewDidAppear like thus:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let center: NotificationCenter = NotificationCenter.default
center.addObserver(self, selector: #selector(keyboardDidShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
center.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}

Related

Get Keyboard Toolbar Height

i have the same question as this :
iPhone Keyboard with accessory view height problems
but the answer has no new solution and does not solve anything!
I found a good solution Here
Made few changes and updated it to swift 4.2.
Few points to be mentioned
Created an outlet of textfield and bottom constraint from Storyboard to ViewController
Bottom constraint is used for moving the textfield up and down.
class ViewController: UIViewController {
#IBOutlet weak var inputField: UITextField!
#IBOutlet weak var textFieldBottomContraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
self.setUpKeyBoardNotifications()
self.addToolBarTo(uiElement: self.inputField)
}
func setUpKeyBoardNotifications()
{
NotificationCenter.default.addObserver(self,
selector: #selector(self.keyboardNotification(notification:)),
name: UIResponder.keyboardWillChangeFrameNotification,
object: nil)
}
func addToolBarTo(uiElement element:UITextField)
{
let numberToolbar = UIToolbar(frame:CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 45))
numberToolbar.barStyle = .black
numberToolbar.items = [
UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(ViewController.cancelAction)),
UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil),
UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(ViewController.doneAction))]
numberToolbar.sizeToFit()
element.inputAccessoryView = numberToolbar
}
#objc func keyboardNotification(notification: NSNotification) {
if let userInfo = notification.userInfo {
let endFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
let endFrameY = endFrame?.origin.y ?? 0
let duration:TimeInterval = (userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
let animationCurveRawNSN = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber
let animationCurveRaw = animationCurveRawNSN?.uintValue ?? UIView.AnimationOptions.curveEaseInOut.rawValue
let animationCurve:UIView.AnimationOptions = UIView.AnimationOptions(rawValue: animationCurveRaw)
if endFrameY >= UIScreen.main.bounds.size.height {
self.textFieldBottomContraint?.constant = 0.0
} else {
self.textFieldBottomContraint?.constant = endFrame?.size.height ?? 0.0
}
UIView.animate(withDuration: duration,
delay: TimeInterval(0),
options: animationCurve,
animations: { self.view.layoutIfNeeded() },
completion: nil)
}
}
#objc func cancelAction()
{
self.inputField.resignFirstResponder()
}
#objc func doneAction()
{
self.inputField.resignFirstResponder()
}
deinit {
NotificationCenter.default.removeObserver(self)
}
}

Swift Unexpectedly found nil while unwrapping an Optional value while pass image to another ViewController

I have two VCs. The first one contains an imagePicker from gallery and a callback function that sends and image to the chatLog... I want to send the selected photo to the 2nd VC
if let selectedImage = selectedImageFromPicker {
//self.callback?(selectedImage)
detailImageViewController.aImage = selectedImage
}
I created the 2nd VC as controller for PreviewImage with buttons cancel or accept. I tried to pass the image displayed on the 2nd VC back to 1st VC this way but it shows me:
Fatal error: Unexpectedly found nil while unwrapping an Optional value.
How can I fix that?
var t : EVTPhotoTekingHelper!
#objc func actionSend() {
if aImage != nil{
t.callback?(aImage!)
}
else {
print("nil")
}
dismiss(animated: true, completion: nil)
}
UPDATED:
My 1st VC
typealias PhotoTekingHelperCallBack = (UIImage?) -> ()
class EVTPhotoTekingHelper: NSObject {
// View controller on which AlertViewController and UIImageViewController are present
weak var viewController: UIViewController!
var imagePickerController: UIImagePickerController?
var callback: PhotoTekingHelperCallBack?
var photoTakinHelper: EVTPhotoTekingHelper!
// MARK: - Initialization
init(viewController: UIViewController, callback: #escaping PhotoTekingHelperCallBack) {
self.viewController = viewController
self.callback = callback
super.init()
showPhotoSourceSelection()
}
func showPhotoSourceSelection() {
let alertController = UIAlertController.init(title: nil,
message: "Message?",
preferredStyle: .actionSheet)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
let photoLibraryAction = UIAlertAction(title: "from library", style: .default) { (action) in
self.showImagePickerController(sourceType: .photoLibrary)
}
alertController.addAction(cancelAction)
alertController.addAction(photoLibraryAction)
if UIImagePickerController.isFlashAvailable(for: .rear) {
let cameraAction = UIAlertAction.init(title: "from camera", style: .default, handler: { (action) in
self.showImagePickerController(sourceType: .camera)
})
alertController.addAction(cameraAction)
}
viewController.present(alertController, animated: true, completion: nil)
}
func showImagePickerController(sourceType: UIImagePickerControllerSourceType) {
imagePickerController = UIImagePickerController.init()
imagePickerController!.sourceType = sourceType
imagePickerController!.delegate = self
viewController.present(imagePickerController!, animated: true, completion: nil)
}
}
Extension from 1st VC
extension EVTPhotoTekingHelper: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
var selectedImageFromPicker: UIImage?
if let editedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
selectedImageFromPicker = editedImage
} else if let originImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
selectedImageFromPicker = originImage
}
let detailImageViewController = EVImagePreviewController()
let ncDetailImageViewController = UINavigationController(rootViewController: detailImageViewController)
if let selectedImage = selectedImageFromPicker {
//self.callback?(selectedImage)
detailImageViewController.aImage = selectedImage
}
viewController.dismiss(animated: false, completion: nil)
viewController.parent?.present(ncDetailImageViewController, animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
viewController.dismiss(animated: false, completion: nil)
}
}
My 2nd VC
class EVImagePreviewController: UIViewController, UIScrollViewDelegate {
var t : EVTPhotoTekingHelper!
var aImageView: UIImageView!
var aImage: UIImage!
private var aScrollView: UIScrollView!
override func viewDidAppear(_ animated: Bool) {
aImageView = UIImageView(frame: CGRect(x: 0, y: 75, width: (aImage?.size.width)!, height: (aImage?.size.height)!))
aImageView.contentMode = .scaleAspectFit
aImageView.image = aImage
aScrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height))
aScrollView.backgroundColor = .clear
aScrollView.contentSize = CGSize(width: view.frame.size.width, height: view.frame.height)
aScrollView.minimumZoomScale = 0.2
aScrollView.maximumZoomScale = 2.3
aScrollView.clipsToBounds = true
aScrollView.delegate = self
aScrollView.addSubview(aImageView)
view.addSubview(aScrollView)
aImageView.center = CGPoint(x: aScrollView.bounds.midX, y: aScrollView.bounds.midY - 35)
}
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(actionSend))
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(actionBack))
}
// MARK: - IBAction
#objc func actionBack() {
dismiss(animated: false, completion: nil)
}
#objc func actionSend() {
print("\(t)")
if aImage != nil{
t.callback?(aImage!)
}
else {
print("nil")
}
//self.callback?(aImage)
dismiss(animated: true, completion: nil)
}
// MARK: - UIScrollViewDelegate
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return aImageView
}
func scrollViewDidZoom(_ scrollView: UIScrollView) {
let subView = scrollView.subviews[0]
let offsetX = max((scrollView.bounds.width - scrollView.contentSize.width) * 0.5, 0.0)
let offsetY = max((scrollView.bounds.height - scrollView.contentSize.height) * 0.5, 0.0)
subView.center = CGPoint(x: scrollView.contentSize.width * 0.5 + offsetX, y: scrollView.contentSize.height * 0.5 + offsetY)
}
}
Here you dismiss your first view controller before you present your 2nd one from the parent of your first view controller:
viewController.dismiss(animated: false, completion: nil)
To me so far doesn't make sense. Its not clear where is the callback implementation is.
My guess that the nil exception is not for the UIImage for sure. its somewhere
inside your callback implementation.

How to pass the variable from a ViewController Class to other Struct?

I have no idea how to pass the data from the class to another struct. This is my ViewController.swift file. And I have another file called Meme.swift which is used to save the struct. I tried to put the struct in ViewController.swift file as well as Meme.swift file but I cannot access the value like topTextField.text and lowTextField.text and use them in the struct. Can anyone help me with this?
ViewController:
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITextFieldDelegate {
#IBOutlet weak var cameraButton: UIBarButtonItem!
#IBOutlet weak var bottomTextField: UITextField!
#IBOutlet weak var topTextField: UITextField!
#IBOutlet weak var imagePickerView: UIImageView!
#IBOutlet weak var imageView: UIImageView!
#IBOutlet weak var actionButton: UIBarButtonItem!
let bottomTextFieldDelegate = BottomTextFieldDelegate()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
topTextField.text = "TOP"
bottomTextField.text = "BOTTOM"
topTextField.delegate = self
bottomTextField.delegate = self.bottomTextFieldDelegate
}
func textFieldDidBeginEditing(_ textField: UITextField) {
topTextField.text = ""
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.view.endEditing(true)
textField.resignFirstResponder()
actionButton.isEnabled = true
return true
}
#IBAction func shareAction(_ sender: Any) {
let image = generateMemedImage()
let controller = UIActivityViewController(activityItems: [image as Any], applicationActivities: nil)
self.present(controller, animated: true, completion: nil)
controller.completionWithItemsHandler = {(activityType: UIActivityType?, completed:Bool, returnedItems:[Any]?, error: Error?) in
if !completed {
debugPrint("cancelled")
return
}else{
self.save()
self.dismiss(animated: true, completion: nil)
}
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
cameraButton.isEnabled = UIImagePickerController.isSourceTypeAvailable(.camera)
configureTextField(textField: topTextField)
configureTextField(textField: bottomTextField)
topTextField.textAlignment = .center
bottomTextField.textAlignment = .center
subscribeToKeyboardNotifications()
actionButton.isEnabled = false
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
unsubscribeFromKeyboardNotifications()
}
func configureTextField(textField: UITextField) {
textField.defaultTextAttributes = memeTextAttributes
}
func save() -> Meme {
// Create the meme
let memedImage = generateMemedImage()
let meme = Meme(topText: topTextField.text!, bottomText: bottomTextField.text!, originalImage: imageView.image!, memedImage: memedImage)
return meme
}
func generateMemedImage() -> UIImage {
// TODO: Hide toolbar and navbar
navigationController?.setToolbarHidden(true, animated: true)
self.navigationController?.isNavigationBarHidden = true
// Render view to an image
UIGraphicsBeginImageContext(self.view.frame.size)
view.drawHierarchy(in: self.view.frame, afterScreenUpdates: true)
let memedImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
// TODO: Show toolbar and navbar
navigationController?.setToolbarHidden(false, animated: false)
self.navigationController?.isNavigationBarHidden = false
return memedImage
}
func keyboardWillShow(_ notification:Notification) {
view.frame.origin.y = 0 - getKeyboardHeight(notification)
}
func keyboardWillHide(_ notification:Notification) {
view.frame.origin.y = 0
}
func getKeyboardHeight(_ notification:Notification) -> CGFloat { //getting the height of keyboard and use it for func keyboardWillShow to relocate
//the keyboard using keyboardWillShow function
let userInfo = notification.userInfo
let keyboardSize = userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue // of CGRect
return keyboardSize.cgRectValue.height
}
func subscribeToKeyboardNotifications() { //setting up the obeserver to be notified when keyboard is shown or not, then execute keyboardWillShow function
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: .UIKeyboardWillHide, object: nil)
}
func unsubscribeFromKeyboardNotifications() { //unsubscribe the notification
NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillHide, object: nil)
}
//setting the arributes of the text
let memeTextAttributes:[String:Any] = [
NSStrokeColorAttributeName: UIColor.black,
NSForegroundColorAttributeName: UIColor.white,
NSFontAttributeName: UIFont(name: "HelveticaNeue-CondensedBlack", size: 40)!,
NSStrokeWidthAttributeName: 3,]
#IBAction func pickAnImageFromAlbum(_ sender: Any) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
present(imagePicker, animated: true, completion: nil)
}
#IBAction func pickAnImageFromCamera(_ sender: Any) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .camera
present(imagePicker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
imagePickerView.image = image
imagePickerView.contentMode = .scaleAspectFit
dismiss(animated: true, completion: nil)
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController){
dismiss(animated: true, completion: nil)
}
}
BottomFieldDelegate:
import Foundation
import UIKit
class BottomTextFieldDelegate: NSObject, UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
textField.text = ""
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
let viewController = ViewController()
textField.resignFirstResponder()
viewController.view.endEditing(true)
let actionButton = viewController.actionButton
actionButton?.isEnabled = true
return true
}
}
First u need to move this function like save() and generateMemedImage() into your ViewController class and then you can access topTextField.text and lowTextField.text
Modify your struct like below, and do not put nsobject in struct
struct Meme {
var topText: String
var bottomText: String
var memedImage: UIImage
var originalImage: UIImage
init(topText: String, bottomText: String, originalImage: UIImage, memedImage: UIImage) {
self.topText = topText
self.bottomText = bottomText
self.originalImage = originalImage
self.memedImage = memedImage
}
}
Remove save() from Meme struct and put that code in your Viewcontroller file.
Modify your function which return Meme struct object.
func save() -> Meme {
// Create the meme
let meme = Meme(topText: topTextField.text!, bottomText: bottomTextField.text!, originalImage: imageView.image!, memedImage: memedImage)
return meme
}
And you can store in Array like below.
let array:[Meme] = []
array.append(save())

Creating Sign Up View page in SWIFT with A UIImage profile picture setup, Using NEXT as Return Key and UIImage setup not working

I am building an app with which my focus is on User Friendly.
In the SignUpViewController, i have a profilePic of type UIImage, Four standard UITextFields to record user's data, and Two more UITextField which activates a UIDatePicker and a UIPicker.
I'm experiencing some problems such as;
1) The UIImage doesn't clip to bounds to give it the round sort of look
2) When i use the UIImage to fetch image from my gallery, it doesn't give me the option to scale my image size and rather gives me a static image pick
3) My UITextFields don't respond to the Next settings i have used. Again, the next setting has been implemented in my LogInViewController and works perfectly. But why isn't it working in the SignUpViewController?
A big thank you in advance.
import UIKit
import Parse
class SignUpViewController: UIViewController, UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet weak var profilePic: UIImageView!
#IBOutlet weak var firstName: UITextField!
#IBOutlet weak var lastName: UITextField!
#IBOutlet weak var signUpEmail: UITextField!
#IBOutlet weak var signUpPassword: UITextField!
#IBOutlet weak var dateTextField: UITextField!
#IBOutlet weak var genderTextField: UITextField!
var datePicker:UIDatePicker!
var genderPicker:UIPickerView!
var genderSelect = ["Male", "Female"]
override func viewDidLoad() {
super.viewDidLoad()
// PROFILE PICTURE
let tapGesture = UITapGestureRecognizer(target: self, action: "imageTapped:")
profilePic.addGestureRecognizer(tapGesture)
profilePic.userInteractionEnabled = true
profilePic.frame = CGRect(x: 10, y: 170, width: 80, height: 80)
profilePic.layer.cornerRadius = profilePic.frame.size.width / 2
profilePic.clipsToBounds = true
// UI DATE PICKER SETUP
var customView:UIView = UIView(frame: CGRectMake(0, 100, 320, 160))
customView.backgroundColor = UIColor.clearColor()
datePicker = UIDatePicker(frame: CGRectMake(0, 0, 320, 160))
datePicker.datePickerMode = UIDatePickerMode.Date
datePicker.maximumDate = NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitYear, value: -16, toDate: NSDate(), options: nil)
customView.addSubview(datePicker)
dateTextField.inputView = customView
var dateToolBar = UIToolbar()
dateToolBar.barStyle = UIBarStyle.Default
dateToolBar.translucent = true
dateToolBar.tintColor = UIColor(red: 246/255, green: 141/255, blue: 17/255, alpha: 1)
dateToolBar.sizeToFit()
dateTextField.inputAccessoryView = dateToolBar
var doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "datePickerSelected")
var spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
var cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "cancelPicker")
dateToolBar.setItems([cancelButton, spaceButton, doneButton], animated: true)
dateToolBar.userInteractionEnabled = true
// UI GENDER PICKER VIEW
genderPicker = UIPickerView(frame: CGRectMake(0, 0, 320, 160))
genderPicker.showsSelectionIndicator = true
var customGenderView:UIView = UIView(frame: CGRectMake(0, 100, 320, 160))
customGenderView.backgroundColor = UIColor.clearColor()
customGenderView.addSubview(genderPicker)
genderTextField.inputView = customGenderView
var toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.Default
toolBar.translucent = true
toolBar.tintColor = UIColor(red: 246/255, green: 141/255, blue: 17/255, alpha: 1)
toolBar.sizeToFit()
genderPicker.delegate = self
genderPicker.dataSource = self
var doneBtn = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "donePicker")
var spaceBtn = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
var cancelBtn = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "cancelPicker")
toolBar.setItems([cancelBtn, spaceBtn, doneBtn], animated: true)
toolBar.userInteractionEnabled = true
genderTextField.inputView = genderPicker
genderTextField.inputAccessoryView = toolBar
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
if let nextField = textField.nextField {
nextField.becomeFirstResponder()
}
return true
}
// UIIMAGE TO FUNCTION AS BUTTON WHEN TAPPED
func imageTapped(gesture:UIGestureRecognizer) {
if let profilePic = gesture.view as? UIImageView {
showActionSheet()
}
}
func camera() {
var myPickerController = UIImagePickerController()
myPickerController.delegate = self
myPickerController.sourceType = UIImagePickerControllerSourceType.Camera
//myPickerController.allowsEditing = true
self.presentViewController(myPickerController, animated: true, completion: nil)
}
func photoLibrary()
{
var myPickerController = UIImagePickerController()
myPickerController.delegate = self
myPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
myPickerController.allowsEditing = true
//myPickerController.setEditing(true, animated: true)
self.presentViewController(myPickerController, animated: true, completion: nil)
}
func showActionSheet() {
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
actionSheet.addAction(UIAlertAction(title: "Camera", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction!) -> Void in
self.camera()
}))
actionSheet.addAction(UIAlertAction(title: "Gallery", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction!) -> Void in
self.photoLibrary()
}))
actionSheet.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
self.presentViewController(actionSheet, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject: AnyObject]) {
profilePic.image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.dismissViewControllerAnimated(true, completion: nil)
}
// DATE FORMATTING AND CHOOSING
func datePickerSelected() {
dateTextField.text = datePicker.date.description
dateTextField.text = self.dateformatterDate(datePicker.date) as String
dateTextField.resignFirstResponder()
}
func dateformatterDate(date: NSDate) ->NSString {
var dateFormatter: NSDateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy"
return dateFormatter.stringFromDate(date)
}
// GENDER SELECT
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView:UIPickerView, numberOfRowsInComponent component: Int) ->Int {
return genderSelect.count
}
func pickerView(pickerView:UIPickerView, titleForRow row:Int, forComponent component:Int) -> String! {
return genderSelect[row]
}
func pickerView(pickerView:UIPickerView, didSelectRow row:Int, inComponent component:Int) {
genderTextField.text = genderSelect[row]
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
return false
}
func genderPickerSelected() {
genderTextField.text = genderPicker.description
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
self.view.endEditing(true)
}
#IBAction func signUpBtn(sender: AnyObject) {
}
}
1.
Tell the imageView to clip to bounds after image assignment. I also suggest altering it's content mode accordingly if desired (aspect fill, fit, etc.)
Edit: Also reset the corner radius.
You need to scale images yourself in your didFinishPickingMediaWithInfo method. There are lots of different ways to do this, about halfway down this page is one I used a few times: http://www.raywenderlich.com/93276/implementing-tesseract-ocr-ios
Did you set the textFieldNameHere.delegate = self anywhere? I couldn't spot it.

Swift UIButton background image not being set by UIImagePicker

I am able to select an Image from the PhotoLibrary but the background image of my UIButton does not change. The println in didFinishPickingImage and imagePickerControllerDidCancel does not show in the console so I do not think those functions are being called.
class AddTeamTableViewController: UITableViewController, UITextFieldDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIAlertViewDelegate {
var picker:UIImagePickerController? = UIImagePickerController()
#IBOutlet var teamNumber: UITextField!
#IBOutlet var schoolName: UITextField!
#IBOutlet var teamImage: UIButton!
#IBAction func cancelButton(sender: AnyObject) {
dismissViewControllerAnimated(true, completion: nil)
}
#IBAction func addTeam(sender: AnyObject) {
var newTeam = Team()
var onlineTeam = PFObject(className: "Team")
// add new team to the list
newTeam.name = teamNumber.text
newTeam.schoolName = schoolName.text
teamList.append(newTeam)
// add online
onlineTeam["name"] = newTeam.name
onlineTeam["fromUser"] = PFUser.currentUser()
onlineTeam["schoolName"] = newTeam.schoolName
onlineTeam.save()
//close the view
dismissViewControllerAnimated(true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return 1
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
self.view.endEditing(true)
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true;
}
#IBAction func pickImage(sender: AnyObject) {
var image = UIImagePickerController()
image.delegate = self
var alert:UIAlertController = UIAlertController(title: "Choose Image", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
var cameraAction = UIAlertAction(title: "Camera", style: UIAlertActionStyle.Default) {
UIAlertAction in
self.openCamera()
}
var galleryAction = UIAlertAction(title: "Gallery", style: UIAlertActionStyle.Default) {
UIAlertAction in
self.openGallery()
}
var cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) {
UIAlertAction in
}
alert.addAction(cameraAction)
alert.addAction(galleryAction)
alert.addAction(cancelAction)
self.presentViewController(alert, animated: true, completion: nil)
/*
alert.delegate = self
alert.message = "Choose Image Source"
alert.addButtonWithTitle("Camera")
alert.addButtonWithTitle("Photo Library")
alert.show()
*/
//image.sourceType = UIImagePickerControllerSourceType.Camera
//image.allowsEditing = false
//self.presentViewController(image, animated: true, completion: nil)
}
func openCamera() {
if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)) {
picker!.sourceType = UIImagePickerControllerSourceType.Camera
self.presentViewController(picker!, animated: true, completion: nil)
} else {
openGallery()
}
}
func openGallery() {
picker!.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.presentViewController(picker!, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
println("Image selected")
self.dismissViewControllerAnimated(true, completion: nil)
teamImage.setBackgroundImage(image, forState: UIControlState.Normal)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController!) {
println("picker cancel")
}
}
The button doesn't seem initialized
let teamImage: UIButton = UIButton(frame: CGRect(x: 0, y: 0, width: 380, height: 300))
let imageTest = UIImage(named: "Hypnotoad")
teamImage.setTitle("HypnotoadTitle", forState: .Normal)
teamImage.setBackgroundImage(imageTest, forState: .Normal)
tested this in a Playground and works ok.
I figured it out. I had 2 different variables of type UIImagePickerController and I was saving to the wrong one. I deleted the first var picker declaration and combined the code to just one UIImagePickerController and it works as intended.