Remove all subsview from a UIView:
view.subviews.forEach({ $0.removeFromSuperview() })
Remove all subsview from a UIView:
view.subviews.forEach({ $0.removeFromSuperview() })
Quick and easy way to simulate UIButton tap, in code:
myButton.sendActionsForControlEvents(.TouchUpInside)
ML Kit translate is a powerful language translation tool, but when translating a passage of text containing line breaks, the returned translated text has the line breaks stripped out.
I’ve tried a bunch of different techniques to maintain line breaks through the translation, and here’s the best so far:
1. Replace line breaks with unicode $ value in text before translating
textToTranslate = textToTranslate.replacingOccurrences(of: "\n", with: "\u{24}")
2. Replace unicode $ value back to line break in returned text after translating
translatedText = translatedText.replacingOccurrences(of: "\u{24}", with: "\n")
The returned translated text now maintains the original line breaks, when displayed in a UITextView for example.
Simple method to trim double:
func roundDouble(value: Double, decimalPlaces: Int) -> Double{
let response = Double(String(format: "%.\(decimalPlaces)f", value))
return response ?? 0.0
}
Usage:
let roundedDouble = roundDouble(value: 3.1415926535897932, decimalPlaces: 2)
print(roundedDouble)
Prints:
3.14
mySwitch.layer.borderColor = UIColor.lightGray.cgColor
mySwitch.layer.borderWidth = 2
mySwitch.layer.cornerRadius = 15
Xcode + developer tools must be installed on your Mac.
1. Open Terminal and run the following command to install Cocoa Pods for Xcode:
sudo gem install cocoapods
Enter your Mac password. The required packages will be downloaded and installed.
2. In Terminal, navigate to the root folder of your Xcode project i.e. the folder containing MyProject.xcodeproj
cd /Users/Robbamforth/Projects/MyProject
3. Run the following command to create an empty initial PodFile
in your Xcode project folder.
pod init
Now you have a PodFile
in your projects root directory.
4. Open the with TextEdit (or other editor), and add your pod details under the ‘#Pods for MyProject’ tag
# Pods for MyProject
pod 'GoogleMLKit/Translate', '3.1.0'
5. Install the pod using the following command in Terminal:
pod install
This will download the libraries required for your pod, install the necessary components, and create a new workspace named MyProject.xcworkspace.
From now on, open the .xcworkspace file from finder to work in xcode. If you open the .xcodeproj file, you will not be able to compile or run your project with cocoa pods references.
You can add additional pods to the PodFile in step 4, then repeat step 5 to install.
Add Notification Center observer in ViewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(orientationChange), name: UIDevice.orientationDidChangeNotification, object: nil)
Add methid to call when device rotated:
@objc func orientationChange() {
//TODO
}
Finally, remove observer when finished with view controller (e.g. add to viewWillDisappear() method):
NotificationCenter.default.removeObserver(self, name: UIDevice.orientationDidChangeNotification, object: nil)
Here is a super simple method to reverse a posative or negative number in swift:
func reversePosNeg(number: Int) -> Int{
return -number
}
Usage:
let reversedNumber = reversePosNeg(number: -99)
print(reversedNumber)
OUTPUT: 99
Change the input type for Float, Double, etc.
How to check device type and run specific code for iPad or iPhone.
witch UIDevice.current.userInterfaceIdiom {
case .phone:
print("phone")
case .pad:
print("ipad")
case .unspecified:
print("unspecified")
case .tv:
print("tv")
case .carPlay:
print("carPlay")
case .mac:
print("mac")
@unknown default:
print("unknown")
}
In this example the presented ViewController will display fullscreen for iPad, else use default presentation style for other devices, i.e. iPhone:
let popover = MyViewController()
switch UIDevice.current.userInterfaceIdiom {
case .phone:
print("phone")
case .pad:
print("ipad")
popover.modalPresentationStyle = .fullScreen
case .unspecified:
print("unspecified")
case .tv:
print("tv")
case .carPlay:
print("carPlay")
case .mac:
print("mac")
@unknown default:
print("unknown")
}
present(popover, animated: true)
Create gesture and add it to your imageview in ViewDidLoad():
let standardPinch = UIPinchGestureRecognizer(target: self, action: #selector(pinch))
standardPinch.delegate = self
myImageView.isUserInteractionEnabled = true
myImageView.addGestureRecognizer(standardPinch)
Add pinch() method to handle resizing of UIImageView:
@objc func pinch(_ sender: UIPinchGestureRecognizer){
guard let targetView = sender.view else {return}
targetView.transform = targetView.transform.scaledBy(x: sender.scale, y: sender.scale)
sender.scale = 1
}