Ambiguous use of 'cornerRadius' in ASAuthorizationAppleIDButton - swift

I want to use "Sign in with Apple" in my app and according to design better to set cornerRadius to half of heoght of the button.
I've tried to test cornerRadius in sample project from Apple: https://developer.apple.com/documentation/authenticationservices/implementing_user_authentication_with_sign_in_with_apple
So, I've tested sample project, here's my code:
#available(iOS 13.0, *)
private func setupProviderLoginView() {
let authorizationButton = ASAuthorizationAppleIDButton(type: .default, style: .whiteOutline)
authorizationButton.cornerRadius = 100
authorizationButton.addTarget(self, action: #selector(handleAuthorizationAppleIDButtonPress), for: .touchUpInside)
self.signInWithAppleView.addSubview(authorizationButton)
authorizationButton.fillSuperview()
}
On sample project all works fine, but on my project there is error on .cornerRaduis line:
Ambiguous use of 'cornerRadius'
What's the problem of it?

This worked for me:
(authorizationButton as UIControl).cornerRadius = 18

Replace
authorizationButton.cornerRadius = 100
with
authorizationButton.layer.cornerRadius = 100

you can quick fix this issue like below: appleBtn.setValue(24, forKey: "cornerRadius").
It's ugly, I cannot find out a better solution.

authorizationButton.setCornerRadius(radius: 25)
Try this, it will work.

Related

How to correctly pass GPUImage ExposureAdjustment "exposure" parameter in Swift?

I am struggling with GPUImage on Swift for a few days now, and I think it is time to ask for help or at least a clue.
What I am trying to do is adjust the exposure of an NSImage with GPUImage, but I have failed miserably on this so far.
My code works for filters like SmoothToonFilter, but I just seem unable to set the exposure parameter for the ExposureAdjustment filter... and I don't have a clue on how to fix this.
Here's what works with smoothSmoothToonFilter:
filteredImage = filteredImage?.filterWithOperation(GPUImage.SmoothToonFilter()
.. while this doesn’t:
filteredImage = filteredImage?.filterWithOperation(GPUImage.ExposureAdjustment() )
It does not accept me to do this either:
filteredImage = filteredImage?.filterWithOperation(GPUImage.ExposureAdjustment(5.0) )
Or this:
filteredImage = filteredImage?.filterWithOperation(GPUImage.ExposureAdjustment(exposure: 5.0) )
While ExposureAdjustment.swift in the GPUImage project clearly (to me, anyways) shows there is a way to set this…
public class ExposureAdjustment: BasicOperation {
public var exposure:Float = 0.0 { didSet { uniformSettings["exposure"] = exposure } }
public init() {
super.init(fragmentShader:ExposureFragmentShader, numberOfInputs:1)
({exposure = 0.0})()
}
}
I am aware this issue is fully the fault of my lack of knowledge, but I really tried solving this on my own up to the point of it driving me absolutely nuts... and I would really appreciate some help so I can move on with my Swift experiments... Thanks in advance!
This line:
public var exposure:Float... denotes a property on the ExposureAdjustment class. You can set it by splitting out the initialization of the filter and setting it in a new statement. Like this:
let exposureFilter = GPUImage.ExposureAdjustment()
exposureFilter.exposure = 5.0
filteredImage = filteredImage?.filterWithOperation(exposureFilter)

Swift:"Unrecognized selector sent to instance", Xcode 9.4.1

Can anyone help me with this?
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate=self
tableView.dataSource=self
searchBar.autocorrectionType = .yes
searchBar.delegate=self
searchBarView.translatesAutoresizingMaskIntoConstraints=false
let tap:UIGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector(("tapCancel:")))
searchBarView.addGestureRecognizer(tap)
tableView.addGestureRecognizer(tap)
tableView.isUserInteractionEnabled=true
}
...
func tapCancel(sender:UITapGestureRecognizer){
hideSearchBarView()
isSearchon=false
print("Tap cancel!")
}
Every time I tap the View, it crash.
"unrecognized selector sent to instance 0x7feb85d109e0"
I will appreciate any help!
Thanks!
I believe the reason is due to how method names get mapped between Swift and Objective-C, which underlies the Cocoa implementation and the whole target/action mechanism.
In your case, the Swift method:
#objc func tapCancel(sender:UITapGestureRecognizer)
...corresponds to the Objective-C selector:
-tapCancelWithSender:
Note: In order to work with the target/action paradigm (i.e., called by means of a selector), the method needs to be declared as #objc. The alternative attribute #IBOutlet (for use in conjunction with Interface Builder) also supports this. (tip of the hat to #rmaddy)
In order to remove the "withSender" part and get a selector that matches tapCancel:, you need to tell Swift to remove the argument label sender, like this:
func tapCancel(_ sender:UITapGestureRecognizer) // Notice the underscore (_)
Also, in line with the comment by #dan, perhaps you can use:
#selector(self.tapCancel(_:))
or more succinctly, as pointed by (thanks again) #rmaddy, just:
#selector(tapCancel)
(Xcode will try to autocomplete it to #selector(tapCancel(_:)), but the shorter syntax works as well and the method name is highlighted)
I was not familiar with the Selector() syntax you used, so I tried playing a bit with it, and behold:
(Selector does not match any method the compiler can "see").
(After adding "withSender", the compiler can match the method, but it suggests using the better #selector(... syntax).
As #rmaddy also pointed out in the comments, using the shorter #selector(doSomething) syntax (no colons, no underscore, no self) also does away with the problem of whether "withSender" is needed or not.
use
let tap:UIGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapCancel))
instead of
let tap:UIGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector(("tapCancel:")))
And add #objc before method tapCancel()

Issue with types in swift?

I did a Team Treehouse code challenge, and I had an issue that I wanted to ask. Basically I created a func inside an enum that takes the specific case of the enum and initializes a given object. It told me to create it inside the enum, but unfortunately when I do this, I came across some errors. I was able to finish the challenge by doing it outside the enum, but I don't think that was exactly what I was supposed to do.
Anyways, my problem was that when it returned the object UIBarButtonItem, if it was assigned to a constant, the type of the constant is "() -> UIBarButtonItem" or depending on how the switch is setup, it could also come out like "(Button) -> UIBarButtonItem". I'm not really sure what the '->' symbol means in this case. Why does it not completely change into the class? What is going on here? In the tutorial right before this, it seems we did the same exact thing, so I'm not sure why it doesn't work.
enum Button {
case Done(String)
case Edit(String)
func toUIBarButtonItem () -> UIBarButtonItem {
switch self {
case .Done: return UIBarButtonItem(title: "Done", style: .Done, target: nil, action: nil)
case .Edit: return UIBarButtonItem(title: "Plain", style: .Plain, target: nil, action: nil)
}
}
}
let done = Button.Done("Done")
let doneButton = Button.toUIBarButtonItem(done)
This is the code that I am running, not including the UIBarButtonItem class. I hope this is enough information to understand what I am asking.
You are calling the toUIBarButtonItem() method on the Button type instead of a Button instance; this is why it does something special: it returns a curried function, which you then bind to done. The result, however, is still a function.
What you probably mean to do is invoke the method on your done object:
let done = Button.Done("Done")
let doneButton = done.toUIBarButtonItem()
A more in-depth explanation can be found under http://oleb.net/blog/2014/07/swift-instance-methods-curried-functions/

swift 2.2: failable initializers in lazy properties

First very appreciate for your help. I just upgraded Xcode yesterday which contains swift 2.2. I've faced a few issues but I fixed them quickly by following the "what's new in swift 2.2" topics from Natashatherobot. But there is one issue I cannot fix. It's about failable initializers of UIFont which was introduced in swift 2.2. Attached is a simple piece of code that will report error in swift 2.2. It might not report the error immediately, until I cleaned the project.
lazy var somelabel: UILabel = {
let label = UILabel()
let font = UIFont(name: "somefont", size: 10) ?? UIFont.systemFontOfSize(10) //this line gave me error
label.font = font
label.text = "Calculating..."
return label
}()
Here is the screenshot of the error
The error is : (name: String, size: CGFloat) -> UIFont' is not convertible to '(name: String, size: CGFloat) -> UIFont?'
I can fix it in two ways:
Method 1: don't put this line:
let font = UIFont(name: "somefont", size: 10) ?? UIFont.systemFontOfSize(10)
in the 'lazy instantiation' closure. (Put it in computed properties reports no error)
Method 2: instead of using:
UIFont(name: "somefont", size: 10)
use the below instead( However I don't think this should be the right approach because it makes the initializer more "objc" style):
UIFont.init(name: "somefont", size: 10)
But I still don't understand why it would report me error in the lazy property closure. I will be very appreciated if someone can give me some explanations.
This might be a bug of the latest version xcode. Those whose project was set up before the upgrade might face this problem. Anyone who is luck enough to face such issue can try to start a new project in swift 2.2 environment.

NSStringDrawingUsesLineFragmentOrigin unresolved?

I'm developing a Swift app in Xcode 6.1 targeting iOS >= 7 and can't use NSStringDrawingUsesLineFragmentOrigin because it's unresolved. I've googled it but all I find are examples that use it, apparently without issue. Current docs on Apple's site show it as valid. Has anyone encountered / solved the same problem?
Edit:
I've got it working with NSStringDrawingOptions.UsesLineFragmentOrigin (thanks Matt) which I had tried yesterday, but it only works with the target context set to nil. Yesterday when I was passing a valid context, anything I passed in "options" showed as unresolved. Strangely, today if I pass a valid context, the compiler gives me a different error -- "NSString is not identical to NSObject" on my font attributes! I wonder if this is just some quirky bug in the Swift compiler? Anyway, here's the code that eventually compiled:
var textStyle = NSMutableParagraphStyle.defaultParagraphStyle().mutableCopy() as NSMutableParagraphStyle
textStyle.alignment = NSTextAlignment.Left
let fontAttributes = [
NSFontAttributeName: UIFont(name: "Georgia", size: tileSize * CLUES_FONT_SCALE)!,
NSForegroundColorAttributeName: UIColor.blackColor(),
NSParagraphStyleAttributeName: textStyle
]
size = text.boundingRectWithSize(
CGSize(width: width, height: 1000),
options: NSStringDrawingOptions.UsesLineFragmentOrigin,
attributes: fontAttributes,
context: nil
)
Note that if I replace:
NSStringDrawingOptions.UsesLineFragmentOrigin
with
NSStringDrawingUsesLineFragmentOrigin
as shown in the Apple docs, I get "use of unresolved identifier".
I just fired up Xcode 6.1, created a Swift iOS project with a deployment target of iOS 7.0, and pasted the following code into my application's didFinishLaunchingWithOptions:
let attrib_string = NSAttributedString(string: "Foo")
let rect = attrib_string.boundingRectWithSize(
CGSize(width: 100, height: 100),
options: .UsesLineFragmentOrigin,
context: nil)
println(rect)
It compiled, linked and ran fine, and printed the result to the console:
(0.0,0.0,20.6777,13.8)
You may need to edit your question to provide a complete example, and the exact error message you're getting. I couldn't see any oddities; using the fully qualified enum value of NSStringDrawingOptions.UsesLineFragmentOrigin worked fine, too.