Tag: swift

how can I expand the hit area of a specific UIButton in Swift?

In my application I have a UIButton that is quite small, so I thought about increasing the hit area of it. I found an extension for that: fileprivate let minimumHitArea = CGSize(width: 100, height: 100) extension UIButton { open override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { // if the button is […]

Swift | Adding constraints programmatically

I’m trying to figure this out since last week without going any step further. Ok, so I need to apply some constraints programmatically in Swift to a UIView using this code: var new_view:UIView! = UIView(frame: CGRectMake(0, 0, 100, 100)); new_view.backgroundColor = UIColor.redColor(); view.addSubview(new_view); var constX:NSLayoutConstraint = NSLayoutConstraint(item: new_view, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: […]

Swift: Split a String into an array

Say I have a string here: var fullName: String = “First Last” I want to split the string base on white space and assign the values to their respective variables var fullNameArr = // something like: fullName.explode(” “) var firstName: String = fullNameArr[0] var lastName: String? = fullnameArr[1] Also, sometimes users might not have a […]

Scroll View in my app doesn't work on iOS 11

My scrollview in app doesn’t scroll. Because it’s UIView and sets as a custom class, and reference UIViewScroll. It’s work iOS 10 and lower versions but doesn’t work on iOS 11. So how can I fix without recreate scroll view. Project created with Xcode 7 and swift 2.2, and updated swift 3 with Xcode 8.

UITextField text change event

How can I detect any text changes in a textField? The delegate method shouldChangeCharactersInRange works for something, but it did not fulfill my need exactly. Since until it returns YES, the textField texts are not available to other observer methods. e.g. in my code calculateAndUpdateTextFields did not get the updated text, the user has typed. […]

Why does `ordinality(of: .day, in: .era, for: date)` give the same result for 2 dates in different time zones?

Consider the following code: import UIKit let date = Date() guard let nycTimeZone = TimeZone(abbreviation: “EST”), let nzTimeZone = TimeZone(abbreviation: “NZDT”) else { fatalError() } var nycCalendar = Calendar(identifier: .gregorian) nycCalendar.timeZone = nycTimeZone var nzCalendar = Calendar(identifier: .gregorian) nzCalendar.timeZone = nzTimeZone let now = Date() let nycDayOfEra = nycCalendar.ordinality(of: .day, in: .era, for: now) let […]

How to determine the current iPhone/device model?

Is there a way to get the device model name (iPhone 4S, iPhone 5, iPhone 5S, etc) in Swift? I know there is a property named UIDevice.currentDevice().model but it only returns device type (iPod touch, iPhone, iPad, iPhone Simulator, etc). I also know it can be done easily in Objective-C with this method: #import <sys/utsname.h> […]

Do Swift-based applications work on OS X 10.9/iOS 7 and lower?

Will Swift-based applications work on OS X 10.9 (Mavericks)/iOS 7 and lower? For example, I have a machine running OS X 10.8 (Mountain Lion), and I am wondering if an application I write in Swift will run on it.

Transition from UIView to SKView with crossdissolve animation messes up the scaleMode

I have a transition in my sprite-kit app from UIView to SKView. I’m also changing view controllers during the transition. Basically I have a short crossdissolve animation from a spalsh screen (the UIView) to game view (the SKView). The problem is that the screen scale or aspectFill or whatever it’s called is all wrong during […]

PlaceMarker, cannot convert value of type:

i’m creating this class import UIKit import GooglePlaces import GoogleMaps class PlaceMarker: GMSMarker { let place: GMSPlace init(place: GMSPlace) { self.place = place super.init() position = place.coordinate icon = UIImage(named: place.types+”_pin”) groundAnchor = CGPoint(x: 0.5, y: 1) appearAnimation = GMSMarkerAnimation.pop } } using for the PlaceMarker, but in the line icon = UIImage(named: place.types+”_pin”) i […]