takePicture() with cameraOverlayView - swift

I'm using cameraOverlayView to create a custom camera. However, I'm not sure how to make the button shoot the photo. The Apple Docs say takePicture() is the function to use. But it does not compile:
Neither attempt works:
let shootPicture = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Camera, target: self, action: takePicture())
let shootPicture = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Camera, target: self, { action -> Void in
takePicture()
})
Updated Attempt:
let shootPicture = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Camera, style: UIBarButtonItemStyle.Plain, target: self, action: "takePicture")
// separate function
func takePicture() {
imagePicker.takePicture()
}
Still getting syntax error: missing argument for parameter 'landscapeImagePhone' in call
But when i add landscapeImagePhone: UIBarButtonSystemItem.Camera, I get "extra argument 'landscapeImagePhone' in call"

For anyone else stuck on this:
Set Bar Item:
let shootPicture = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Camera, target: self, action: "takePicture")
Create Function
func takePicture() {
imagePicker.takePicture()
}

Related

How to set textField becomeFirstResponder via selector

I wrote a UITextfield extension to build my own toolbar for custom inputs:
func addToolbar(selector: Selector? = nil) {
let action = selector == nil ? #selector(resignFirstResponder) : selector
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: action)
let toolbar = UIToolbar()
toolbar.items = [flexibleSpace, doneButton]
toolbar.sizeToFit()
toolbar.layoutIfNeeded()
inputAccessoryView = toolbar
returnKeyType = .done
}
I use it like this:
let myPickerView = UIPickerView()
myPickerView.dataSource = myPickerViewBehavior
myPickerView.delegate = myPickerViewBehavior
if let selected = value {
let selectedIndex = PickableItemType.index(of: selected.rawValue)
myPickerView.selectRow(selectedIndex, inComponent: 0, animated: true)
}
myTextField.delegate = self
myTextField.inputView = myPickerView
myTextField.addToolbar(selector: #selector(nextTextField.becomeFirstResponder))
As you can imagine I want the next UITextField to becomeFirstResponder after the user pressed done in the toolbar.
But nothing happening.
For testing I've tried using a simple #objs func with print statement and/or nextTextField.becomeFirstResponder inside, same result.
I've also tried to use UITapGestureRecognizer with #selector(nextTextField.becomeFirstResponder), also same result.
So I guess it has something to do with the #selector mechanic why the code doesn't work.
P.S.: if selector == nil inside addToolbar , #selector(resignFirstResponder) works fine.
I believe the problem is caused by the wrong target: while setting up doneButton.
If you also supply the target in addToolbar: method like:
func addToolbar(target: Any? = nil, selector: Selector? = nil) {
let object = target ?? self
let action = selector ?? #selector(resignFirstResponder)
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: object, action: action)
let toolbar = UIToolbar()
toolbar.items = [flexibleSpace, doneButton]
toolbar.sizeToFit()
toolbar.layoutIfNeeded()
inputAccessoryView = toolbar
returnKeyType = .done
}
This should work:
firstTextField.addToolbar(target: secondTextField, selector: #selector(becomeFirstResponder))
Here's a small test result:
https://imgur.com/R7R1ee9

How to Resize UIToolBar from a UIDatePicker?

I'm trying to show a UIDataPicker when the user clicks on the UITextField. It works. However, the size of it seems ridiculous.
Please, check this image from the simulator
It gives a warning regarding the constraints I've used in the storyboard:
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x6000036017c0 'TB_Trailing_Trailing' H:[_UIModernBarButton:0x7fa478418640'Cancel']-(0)-| (active, names: '|':_UIButtonBarButton:0x7fa478418470 )>
func showDatePicker() {
// Format date
bdPicker.datePickerMode = .date
//ToolBar
let toolbar = UIToolbar();
toolbar.sizeToFit()
let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(donedatePicker));
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelDatePicker));
toolbar.setItems([doneButton,spaceButton,cancelButton], animated: false)
BDTextField.inputAccessoryView = toolbar
BDTextField.inputView = bdPicker
}
I tried to modify: toolbar.sizeThatFits() and I gave random values, but the app crashed. So, I'm looking for a strategy that I can make this UIDatePicker fits half of the page at least.

xcode swift nulpad with next button not working

I am relatively new to coding so please bear with me. I have written a simple app which has 6 text fields that need to completed before an action button is pressed to perform a mathematical calculation.
I have everything done but i can't get my next button to move to the next text field. I have added the tool bar and the done and next button but when i press the next button the app crashes and i get a "Thread 1: signal SIGABRT".
I have been through the internet and I am struggling to see how do do it. I would have thought this would be pretty straight forward.
I just can't make the next button perform the action to the next box.
Here is my code
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
//IBOutlet UITextFields are here
override func viewDidLoad() {
super.viewDidLoad()
let toolBar = UIToolbar()
toolBar.barStyle = .default
toolBar.isTranslucent = true
toolBar.sizeToFit()
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
let fixedSpaceButton = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
let doneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action: #selector(self.doneClicked))
let nextButton = UIBarButtonItem(title: "Next", style: .plain, target: self, action: #selector(self.textFieldShouldReturn(_:)))
toolBar.setItems([fixedSpaceButton, nextButton, flexibleSpace, doneButton], animated: false)
toolBar.isUserInteractionEnabled = true
box1.delegate = self
box2.delegate = self
box3.delegate = self
box4.delegate = self
box5.delegate = self
box6.delegate = self
box1.becomeFirstResponder()
}
func doneClicked() {
view.endEditing(true)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool
{
switch textField
{
case box1: box2.becomeFirstResponder()
break
case box2: box3.becomeFirstResponder()
break
case box3: box4.becomeFirstResponder()
break
case box4: box5.becomeFirstResponder()
break
case box5: box6.becomeFirstResponder()
break
default: textField.resignFirstResponder()
}
return true
}
//IBAction for maths calculations are here
I appreciate any help in advance. I literally have no hair left.
Thanks

Pass a function to a #selector

I get a function as function parameter and want to set this in a #selector.
But I get the error message:
Argument of '#selector' cannot refer to a property
I have the following function:
private func addGestureRecognizerToItem(selector: () -> ()) {
let labelGesture = UITapGestureRecognizer(target: self, action: #selector(selector))
let imageGesture = UITapGestureRecognizer(target: self, action: #selector(selector))
label.addGestureRecognizer(labelGesture)
imageView.addGestureRecognizer(imageGesture)
}
Any ideas how to handle this?
How about this?
class ViewController: UIViewController {
let label = UILabel()
let imageView = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
addGestureRecognizerToItem(#selector(test))
}
func test() {
}
private func addGestureRecognizerToItem(selector: Selector) {
let labelGesture = UITapGestureRecognizer(target: self, action: selector)
let imageGesture = UITapGestureRecognizer(target: self, action: selector)
label.addGestureRecognizer(labelGesture)
imageView.addGestureRecognizer(imageGesture)
}
}
It's not possible, rather you can call your desired function from the return of the first function whose data you want to pass to the other one.
And your scenario may change according to your requirement.

UIBarButton not changing

#IBOutlet weak var playStopButton: UIBarButtonItem!
var playStopArray = [UIBarButtonSystemItem.Pause, UIBarButtonSystemItem.Play]
var index = 0
#IBOutlet weak var image: UIImageView!
#IBAction func playButton(sender: UIBarButtonItem) {
println("pressed")
playStopButton = UIBarButtonItem(barButtonSystemItem: playStopArray[index], target: self, action: "startMusic:")
println("here")
if index == 0 {
index = 1
}
else {
index = 0
}
}
func startMusic() {
println("test")
}
I expected the bar button to change to the pause symbol, but with no luck. It prints both "pressed" and "here" but "test" does not work. Why is the image not changing?
Your approach is wrong.
In the following line,
playStopButton = UIBarButtonItem(barButtonSystemItem: playStopArray[index], target: self, action: "startMusic:")
you are actually creating a new instance of UIBarButtonItem. This button is not actually added into the view. Instead of adding the UIBarButtonItem through Interface Builder. You can create it programmatically.
Read this question for more information.
toggle between UIBarButtonSystemItemPlay and UIBarButtonSystemItemPause
var playButton:UIBarButtonItem!
var pauseButton:UIBarButtonItem!
func setup()
{
playButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "startMusic:")
pauseButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "stopMusic:")
}
func startMusic:(button : UIBarButtonItem)
{
self.navigationItem.rightBarButtonItem = pauseButton // Switch to pause.
//Other code.
}
func stopMusic:(button : UIBarButtonItem)
{
self.navigationItem.rightBarButtonItem = playButton// Switch to play.
//Other code.
}
Here is my approach in Swift
func configureBars() {
isPlaying = false
// UIToolbar
let pickBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Search,
target: self,
action: "pickSong")
let playBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Play,
target: self,
action: "playPause")
let pauseBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Pause,
target: self,
action: "playPause")
let leftFlexBBI = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace,
target: nil,
action: nil)
let rightFlexBBI = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace,
target: nil,
action: nil)
playItems = [pickBarButtonItem, leftFlexBBI, playBarButtonItem, rightFlexBBI]
pauseItems = [pickBarButtonItem, leftFlexBBI, pauseBarButtonItem, rightFlexBBI]
toolbar.items = playItems
}
visit http://www.raywenderlich.com/36475/how-to-make-a-music-visualizer-in-ios
or you can check out my Github https://github.com/Charles-Hsu/MusicVisualizer