Cannot create a UIButton using declaration - uibutton

I am trying to create a UIButton by trying to initialise in a viewController as shown below. However, I am getting cannot convert value of type () -> _ to specified type UIButton error which I don't have idea how I stuck into.
let loginRegisterButton : UIButton = {
let button = UIButton(type: .System)
button.backgroundColor = UIColor(r: 80, g: 101, b: 161)
button.setTitle("Register", forState: .Normal)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}
This is the sample code and I have used a UIColor extension and I have attached the screenshot of error herewith.
As far as I know, this is not a syntax error, but I have no idea what I am missing.

you forgot to add
()
at the end
Use Below code ,
let loginRegisterButton : UIButton = {
let button = UIButton(type: .System)
button.backgroundColor = UIColor(r: 80, g: 101, b: 161)
button.setTitle("Register", forState: .Normal)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
What you are missing is mentioned in the error message, that you are assigning closure to UIButton instance. By adding () this will create instance of UIButton.

Related

Swift 3 Button in UICollectionViewCell not respond to touch

I have an UICollectionViewCell and two buttons inside that, but the buttons not respond to touch, i create button programmatically and add that to cell by addSubView it works, but when i added by holder.addSubView not working!
UserInteractionEnabeld of holder is true!
help please
class FavoriteProductCollectionViewCell: UICollectionViewCell {
#IBOutlet weak var holder: UIView!
override func awakeFromNib() {
super.awakeFromNib()
let btnFindBestPrice = UIButton(frame: CGRect(x: 0, y: 0, width: 151, height: 20))
btnFindBestPrice.setTitle("Find best price", for: .normal)
btnFindBestPrice.backgroundColor = UIColor.ButtonBackGreen
btnFindBestPrice.isUserInteractionEnabled = true
btnFindBestPrice.addTarget(self, action: #selector(findBestPrice), for: .touchUpInside)
// AddTarget not working
holder.addSubview(btnFindBestPrice)
// AddTarget works fine
addSubview(btnFindBestPrice)
}
}
I dont know why!? But i create another cell with same config that worked fine!!!!
In this case, it's better if you add self at the beginning. Like this:
self.holder.addSubview(btnFindBestPrice)

Swift 2.3 Button to open URL failing with error unrecognised selector

I have created a button to open url links and am failing with an unrecognised selector error. I would normally set the add target to be self just through how i have read online, but for this particular instance i get the error:
cannot convert value of type NSOBJECT ->() -> infoViewcontroller.infoview to expected argument type AnyObject.
So to get around this and what Xcode recommended was to set the target as NSOBJECT.self. However this no longer got an error but crashes upon the click of the button and returns the reason of: [NSObject displayWebLink]: unrecognized selector sent to class 0x106011e58. So i'm just wondering what the correct way of doing this would be and why? Below is my code.
class ActivityInfoView: UICollectionViewCell {
var activity: ActivitysEvents? {
didSet {
}
}
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
var textView: UITextView = {
let tv = UITextView()
tv.userInteractionEnabled = false
return tv
}()
let dividerLineView: UIView = {
let view = UIView()
view.backgroundColor = UIColor.lightGrayColor()
return view
}()
let urlLinkButton: UIButton = {
let button = UIButton()
button.backgroundColor = UIColor.redColor()
button.titleLabel?.font = UIFont.systemFontOfSize(14)
button.setTitleColor(UIColor.blueColor(), forState: .Normal)
// button.addTarget(self, action: #selector(displayWebLink), forControlEvents: .TouchUpInside)
button.addTarget(NSObject.self, action: #selector(displayWebLink), forControlEvents: .TouchUpInside)
return button
}()
func displayWebLink() {
print("abcdefghijklmnop")
if let urlLink = activity?.url {
// UIApplication.sharedApplication().openURL(NSURL(string: urlLink)!)
UIApplication.sharedApplication().openURL(NSURL(string: urlLink)!, options: [:], completionHandler: nil)
print("dhudududuhdu")
}
}
`
That's not a very helpful error message. The compiler is trying to verify that the correct object defines a method with the name displayWebLink and it seems to be using the closure type as the context where it's searching.
Try telling it where to find the method:
button.addTarget(self, action: #selector(ActivityInfoView.displayWebLink), forControlEvents: .TouchUpInside)
I fixed this issue by changing the button from 'let' to 'lazy var' as below:
lazy var urlLinkButton: UIButton = {
let button = UIButton()
button.backgroundColor = UIColor.redColor()
button.titleLabel?.font = UIFont.systemFontOfSize(14)
button.setTitleColor(UIColor.blueColor(), forState: .Normal)
button.addTarget(self, action: #selector(displayWebLink), forControlEvents: .TouchUpInside)
//button.addTarget(self(), action: #selector(ActivityInfoView.displayWebLink), forControlEvents: .TouchUpInside)
return button
}()

Swift 3 Initializing buttons

Where does one put button initialization code for controls. It won't work in ViewController.swift correct? because I need it to init immediately, not after a button is pressed.
For example I want rounded corners on the buttons.
#IBAction func buttonAdd(_ sender: AnyObject) {
let button = buttonAdd!
button.backgroundColor = UIColor.cyan
// button.backgroundColor = .clear
button.layer.cornerRadius = 15
button.layer.borderWidth = 1
button.layer.borderColor = UIColor.black.cgColor
}
However the this let statement won't work outside of the function either.
Do I need to put init code in a function?
Where would I call the function from?
'buttonAdd' is a function, not a button.
try this code.
if let button = sender as? UIButton
{
button.backgroundColor = UIColor.cyan
// button.backgroundColor = .clear
button.layer.cornerRadius = 15
button.layer.borderWidth = 1
button.layer.borderColor = UIColor.black.cgColor
}
So, you want to init the button with roundedCorner and border.
There's a 'viewDidLoad' function that called only once in view controller's life cycle.
If you want to do something only once, do it in 'viewDidLoad' function.
override func viewDidLoad()
{
super.viewDidLoad()
let btn = UIButton(type: .custom)
btn.frame = .init(x: 100, y: 100, width: 100, height: 30)
btn.setTitle("Hello", for: .normal)
btn.backgroundColor = UIColor.cyan
btn.layer.cornerRadius = 15
btn.layer.borderColor = UIColor.black.cgColor
btn.layer.borderWidth = 1
// you must call this for rounded corner
btn.layer.masksToBounds = true
self.view.addSubview(btn)
}
If you created your button in Interface Builder, You can do it like this.
#IBOutlet weak var btn: UIButton!
override func viewDidLoad()
{
super.viewDidLoad()
btn.layer.cornerRadius = 15
btn.layer.borderColor = UIColor.black.cgColor
btn.layer.borderWidth = 1
// you must call this for rounded corner
btn.layer.masksToBounds = true
}
I found the answer to my question...You would put button init code in ViewController.Swift...in the following function created automatically by the View Controller and you must add button Outlet in addition to button Action.
override func viewDidLoad() {
super.viewDidLoad()
let button = buttonAdd!
button.backgroundColor = UIColor.cyan
// button.backgroundColor = .clear
button.layer.cornerRadius = 10
button.layer.borderWidth = 1
button.layer.borderColor = UIColor.black.cgColor

ios 10 Snapshotting a view that has not been rendered results in an empty snapshot

this question ask again but i dont find for ios 10
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)
{
self.imagePicker.delegate = self
self.imagePicker.sourceType = UIImagePickerControllerSourceType.camera;
self.imagePicker.allowsEditing = false
self.imagePicker.cameraCaptureMode = .photo
//self.imagePicker.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
self.present(self.imagePicker, animated: true, completion: nil)
self.imagePicked.isUserInteractionEnabled = true
}
else
{
print("No Camera")
}
Snapshotting a view that has not been rendered results in an empty
snapshot.Ensure your view has been rendered at least once before
snapshotting or snapshot after screen updates.
when i rotate the camera and take a shot than this error occurs.
Self Solution Working for me like charm :-) hope its helpful for all
DispatchQueue.global(qos: .userInitiated).async
{
self.present(self.imagePicker, animated: true, completion: nil)
}
I got the error
This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes...
Using DispatchQueue.main.async instead works for me.
This behavior is not limited to UIImagePickerController. Below is an example of a UIViewController which presents another UIViewController modally. In the second UIViewController, Safari is launched to present a URL, thus triggering the same error message, "Cannot snapshot view (>) with afterScreenUpdates:NO, because the view is not in a window. Use afterScreenUpdates:YES."
I haven't yet found any way of suppressing the message, but in my app it does no harm. I think what's going on here is that some Apple code is taking a snapshot of the app's view hierarchy, but the keyboard (which is owned by a separate UIWIndow) has not been rendered before the snapshot is taken.
/* Generates the error message:
Cannot snapshot view (<UIKeyboardImpl: 0x7f82ded12ea0; frame = (0 0; 414 271); layer = <CALayer: 0x610000035e20>>) with afterScreenUpdates:NO, because the view is not in a window. Use afterScreenUpdates:YES.
... after the "Now Tap Me, For Glory!" label is clicked
*/
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let input = UITextField()
input.placeholder = "Tap here first -- to bring up keyboard"
input.frame = CGRect(x: 10, y: 50, width: 300, height: 20)
view.addSubview(input)
let button = UIButton()
button.setTitleColor(UIColor.blue, for: .normal)
button.setTitle("Then tap here", for: .normal)
button.addTarget(self,
action: #selector(buttonPushed),
for: .touchUpInside)
button.frame = CGRect(x: 10, y: 80, width: 200, height: 20)
view.addSubview(button)
}
func buttonPushed() {
let modalVC = ModalViewController()
present(modalVC, animated: true, completion: nil)
}
}
class ModalViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
let label = UILabel()
label.text = "Now Tap Me, For Glory!"
label.frame = CGRect(x: 10, y: 50, width: 300, height: 20)
view.addSubview(label)
label.isUserInteractionEnabled = true
label.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(labelTapped)))
}
func labelTapped() {
UIApplication.shared.open(URL(string: "http://planetbeagle.com")!, options: [:], completionHandler: { _ in
self.dismiss(animated: true, completion: nil) })
}
}

Animating only the image in UIBarButtonItem

Ive seen this effect in 2 apps and I REALLY want to find how to do it.
The animation is in a UIBarButtonItem, and is only to the image. The image is a + symbol, and it rotates to a X.
If you want to see the effect you have to start a conversation with someone and next to the text input theres the + button for images and emoji's. Or heres a video of the effect in another app, after he taps the bar button you see it rotate to a X, http://www.youtube.com/watch?v=S8JW7euuNMo.
I have found out how to do the effect but only on a UIImageView, I have to turn off all the autoresizing and the view mode has to be centered, then apply the rotation transform to it. I have tried many ways of trying to have it work in a bar item and so far the best way is adding a image view instance, then setting it up and setting the view mode centered and autoresizing off and then using that image view for a custom bar item view. But when i do this, the effect works except while its doing it, the image will go off to the side a little bit instead of staying where it already is. Ive tried getting the center before the animation and set it during the animation but that doesnt do anything.
So the answer for this is you have to make a instance of the Image view, then set it up with no resizing and view mode is centered. Then add the image view to a UIButton with custom type, and then use the button as the custom view for the bar item.
- (IBAction)animate {
[UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
imageView.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(45));
} completion:^(BOOL finished) {
imageView.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(0));
if ([imageView.image isEqual:[UIImage imageNamed:#"Add.png"]]) {
imageView.image = [UIImage imageNamed:#"Close.png"];
}
else imageView.image = [UIImage imageNamed:#"Add.png"];
}];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Add.png"]];
imageView.autoresizingMask = UIViewAutoresizingNone;
imageView.contentMode = UIViewContentModeCenter;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 40, 40);
[button addSubview:imageView];
[button addTarget:self action:#selector(animate) forControlEvents:UIControlEventTouchUpInside];
imageView.center = button.center;
barItem = [[UIBarButtonItem alloc] initWithCustomView:button];
navItem.rightBarButtonItem = barItem;
}
Recently had to do the same thing in Swift. I created a tutorial that includes starter and final projects, and goes step-by-step with some tips sprinkled in. The code looks like this:
#IBOutlet weak var rightBarButton: UIBarButtonItem! {
didSet {
let icon = UIImage(named: "star")
let iconSize = CGRect(origin: CGPointZero, size: icon!.size)
let iconButton = UIButton(frame: iconSize)
iconButton.setBackgroundImage(icon, forState: .Normal)
rightBarButton.customView = iconButton
rightBarButton.customView!.transform = CGAffineTransformMakeScale(0, 0)
UIView.animateWithDuration(1.0,
delay: 0.5,
usingSpringWithDamping: 0.5,
initialSpringVelocity: 10,
options: .CurveLinear,
animations: {
self.rightBarButton.customView!.transform = CGAffineTransformIdentity
},
completion: nil
)
iconButton.addTarget(self, action: "tappedRightButton", forControlEvents: .TouchUpInside)
}
}
func tappedRightButton(){
rightBarButton.customView!.transform = CGAffineTransformMakeRotation(CGFloat(M_PI * 6/5))
UIView.animateWithDuration(1.0) {
self.rightBarButton.customView!.transform = CGAffineTransformIdentity
}
}
I wanted to keep the expanded tapping size that the native UIBarButtonItem view provides (such as -initWithBarButtonSystemItem:target:action: versus -initWithCustomView:).
Here's a basic implementation of my code.
- (void)setup {
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(navigationBarRightAction)];
}
- (void)navigationBarRightAction {
UIView *itemView = [self.navigationItem.rightBarButtonItem performSelector:#selector(view)];
UIImageView *imageView = [itemView.subviews firstObject];
if (self.shouldRotate) {
imageView.contentMode = UIViewContentModeCenter;
imageView.autoresizingMask = UIViewAutoresizingNone;
imageView.clipsToBounds = NO;
imageView.transform = CGAffineTransformMakeRotation(M_PI_4);
} else {
imageView.transform = CGAffineTransformIdentity;
}
}
You don't have to use a button as a custom view, it works in fact with less code using a UIImageView and adding a UITapGestureRecognizer.
I hope my solution below helps someone b/c I struggled with this for a long time until I got the bar button item to receive taps and get it to work with all the features I wanted. In my case, I made an "alert bell" bar button item that jingles when there are notifications, and then segues to a new tableview controller when tapped.
This was my solution (Swift 5):
#IBOutlet weak var notifyBell: UIBarButtonItem!
func updateNumNotesAndAnimateBell(_ numNotes: Int) {
guard let image = UIImage(named: "alertBellFill_\(numNotes)") else { return }
let imageView = UIImageView(image: image)
notifyBell.customView = imageView
notifyBell.customView?.contentMode = .center
let tap = UITapGestureRecognizer(target: self, action: #selector(notifyBellPressed))
notifyBell.customView?.addGestureRecognizer(tap)
let scaleTransformA = CGAffineTransform(scaleX: 0.8, y: 0.8)
let rotateTransformA = CGAffineTransform(rotationAngle: 0.0)
let hybridTransformA = scaleTransformA.concatenating(rotateTransformA)
let rotateTransformB = CGAffineTransform(rotationAngle: -1*CGFloat.pi*20.0/180.0)
let hybridTransformB = scaleTransformA.concatenating(rotateTransformB)
notifyBell.customView?.transform = hybridTransformA
UIView.animate(withDuration: 3,
delay: 1,
usingSpringWithDamping: 0.1,
initialSpringVelocity: 10,
options: [.allowUserInteraction, .curveEaseInOut],
animations: {
self.notifyBell.customView?.transform = numNotes > 0 ? hybridTransformB : scaleTransformA
},
completion: nil
)
}
#objc func notifyBellPressed(_ sender: UIBarButtonItem) {
performSegue(withIdentifier: "goToNotificationsTVC", sender: self)
}
Key discoveries for me were that:
-- .allowUserInteraction must be included in the animate options, otherwise the UIBarButtonItem won't be active until the animation completes.
-- You will likely have to declare YourBarButtonItem.customView?.contentMode = .center when using CGAffineTransform(rotationAngle: ) or else it will distort your image when it tries to rotate.
-- The code above includes a scale animation and rotate animation that is different depending on how many notifications I have. With zero notifications, the image is an empty bell, else, it displays the number of notifications in the bell image. I probably could've done this with an updating label, but I had already gone the route of making separate PNGs for each so this worked nicely.