Using LOTAnimationView with Auto Layout in Storyboard - ios

Is there a way to use LOTAnimationView (Lottie framework) with auto layout? I tried setting the class of a UIView to LOTAnimationView in my storyboard, but getting nil IB outlet..

Should be set like this in latest Lottie version:
and set animation name here:
and here is example code:
import UIKit
import Lottie
class ViewController: UIViewController {
#IBOutlet weak var animationView: AnimationView!
override func viewDidLoad() {
super.viewDidLoad()
animationView.play()
}
}

Take an outlet of LOTAnimatedControl.
Assign your JSON file to LOTAnimationView
lot.animationView.setAnimation(named: "checked_done_")
Complete example:
import UIKit
import Lottie
class ViewController: UIViewController {
#IBOutlet weak var lot: LOTAnimatedControl!
override func viewDidLoad() {
super.viewDidLoad()
lot.animationView.setAnimation(named: "youranimationjsonfile")
lot.animationView.play()
}
}
Hope this will help you.

Update for the latest Lottie version with Ashish Kakkad's comment.
Change class name AnimationView instead of LOTAnimatedControl,
and then set property animationName from Interface Builder

Related

How to use "MDCOutlinedTextField" in Swift

I need textfield like below image
so i am using material-components-ios framework, for that i have added textfield in storyboard and given its class name to MDCTextField and code
import UIKit
import MaterialComponents
class LoginVC: UIViewController {
#IBOutlet weak var emailTF: MDCTextField!
var textFieldControllerFloating: MDCTextInputControllerOutlined?
override func viewDidLoad() {
super.viewDidLoad()
// setupEditText()
textFieldControllerFloating = MDCTextInputControllerOutlined(textInput: emailTF)
textFieldControllerFloating?.activeColor = UIColor.lightGray
textFieldControllerFloating?.floatingPlaceholderActiveColor = UIColor.green
textFieldControllerFloating?.normalColor = UIColor.lightGray
textFieldControllerFloating?.inlinePlaceholderColor = UIColor.lightGray
}
with this code o/p coming perfect but showing warning
warning:
'MDCTextField' is deprecated: MDCTextField and its associated classes are deprecated. Please use TextControls instead.
so i have added another framework TextControls here i have added pod 'MaterialComponents/TextControls+OutlinedTextFields' and added textfield in stroyboard and given its class to MDCOutlinedTextField and added below code
import UIKit
import MaterialComponents.MaterialTextControls_OutlinedTextFields
class LoginVC: UIViewController {
#IBOutlet weak var emailTF: MDCOutlinedTextField!
#IBOutlet weak var passwordTF: MDCOutlinedTextField!
}
}
but o/p like below: no floating placeholder text and small height as well why?, where am i wrong.. how to get textfield with height 50 and floating placeholder border like above image, please do help.
EDIT: i need eye button for password textfield like this how to do with MDCOutlinedTextField?
As per example try to set label.text property like that
textField.label.text = "Phone number"
As for text field height you can set it for instance with a constraint in Interface Builder

Add PHLivePhotoView to the storyboard

I'm trying to put a PHLivePhotoView into the WelcomeVideoItemViewController using Storyboard. For this purpose the view with a custom class is configured:
The scene hierarchy:
The implementation of the custom View Controller:
class WelcomeVideoItemViewController: UIViewController, PHLivePhotoViewDelegate {
#IBOutlet weak var livePhotoView: PHLivePhotoView!
#IBOutlet weak var tipImageView: UIImageView!
#IBOutlet weak var tipLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
print(livePhotoView)
// Optional(<UIView: 0x7fd161f200b0; frame = (0 0; 320 568); autoresize = RM+BM; layer = <CALayer: 0x60000134ac60>>)
// Crash here
livePhotoView.delegate = self
}
}
So the problem is that the livePhotoView is initialized with a UIView instance, which leads to crash because the UIView has no delegate property. Ho can I make this custom class work?
Xcode 10.2, iOS 12.2, Swift 4.2
I found a workaround for this. First, create a custom PHLivePhotoView subclass:
import PhotosUI
class LivePhotoView: PHLivePhotoView {
}
Then, drag a View to the storyboard and set the custom class for it:
Don't forget to check Inherit Module From Target.

Kingfisher iOS image not loading

I am trying to dynamically set a UIImageView using Kingfisher. The context is there is a TableView on the previous screen of different selectable rows and then when the row is selected a details UIView shows which has more information, including an image. I have gone to the Kingfisher documentation and found that the declaration belongs in the viewDidLoad as i have it but i still cannot get the image to load. I have also verified with debugging that the URL being passed to the Kingfisher is a link to a picture. I am thinking this may have to do with the image being set on a background thread but i am unsure how to fix that if that is the case. I have tried moving the implementation of the Kingfisher to the viewWillAppear and viewDidAppear functions and they still do not display the image even after an extended amount of wait time
Here is my code with some unrelated functionality removed.
import UIKit
import Kingfisher
class DetailsViewController: UIViewController {
#IBOutlet weak var thumbnail: UIImageView!
var thumbnailURL: URL?
override func viewDidLoad() {
super.viewDidLoad()
self.thumbnail.kf.setImage(with: thumbnailURL)
...................................................
// self.thumbnail.image = UIImage(named: "image1")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
kd02 - I have found the answer to my question. When you asked if it was http or https i remembered that you have to enable http in xcode 9 now because of the security enhancement. I did that and now it works.
Try something like this
import UIKit
import Kingfisher
class DetailsViewController: UIViewController {
#IBOutlet weak var thumbnail: UIImageView!
var thumbnailURL: URL?
override func viewDidLoad() {
super.viewDidLoad()
if let url = self.thumbnailURL {
self.thumbnail.setImage(with: url)
}
}
}
I have feeling the optional URL is causing the problem.

Changing an image using swift for iOS App

So I am trying to load a picture programmatically using Swift and I am having some trouble. I can change the UIImage of a view using the Attributes Inspector, so I know the picture I am using is added properly and I am using the right name.
Here is the code I am using:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var myPicture: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
myPicture.image = UIImage(named:"myNewPic")
}
}
I think there might be some setting I am missing in Attributes inspector or something since the code is pretty simple and I know the image is added properly.
Any help would be appreciated.
Make sure that your image name is correct. You have image properly in xcode and your outlet is properly connected
add image with extension if you are not using assets for images. like myNewPic.png or myNewPic.jpg whatever extension it is.
So, your code should be like,
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var myPicture: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
myPicture.image = UIImage(named:"myNewPic.png") //or .jpg or else
}
}
another choice
if you load image from assets usemyNewPic if you load from bundle use myNewPic.png (with extensions).
if let img = UIImage(named: "myNewPic") {
myPicture.image = img
} else {
myPicture.image = UIImage(named:"myNewPic.Imageextensions")
}

Delegate declared in UIView subclass won't work in UIViewController?

I'm trying to create a reusable UIView that I can place in multiple UIViewControllers. I gave it delegate methods that I want the parent UIViewControllers to access, but it throws me an error (commented in the code below). What's a better way I can solve this?
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var cameraView: CameraView!
override func viewDidLoad() {
super.viewDidLoad()
self.cameraView.delegate = self
//ERROR: Cannot assign a value of type 'viewController' to a value of type 'CameraViewDelegate?'
}
}
protocol CameraViewDelegate {
func cameraViewShutterButtonTapped()
func cameraViewimagePickerTapped(imageData: NSData)
}
class CameraView: UIView {
var delegate:CameraViewDelegate?
//Ect...
}
You have not specified that ViewController conforms to the CameraViewDelegate protocol. You should amend your code to this:
class ViewController: UIViewController, CameraViewDelegate {
…at which point Xcode will complain that you have not implemented cameraViewShutterButtonTapped() and cameraViewimagePickerTapped(), which at least tells you that you're on the right track!
Side note: do you really want the camera view to have a strong reference to its delegate? You might want that to be weak.
You need to have your ViewController class implement the CameraViewDelegate protocol, like so:
class ViewController : UIViewController, CameraViewDelegate { ... }

Resources