Add PHLivePhotoView to the storyboard - ios

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.

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

Loading UIView from XIB using Storyboard - view's properties are nil [duplicate]

This question already has answers here:
Load view from an external xib file in storyboard
(11 answers)
Closed 3 years ago.
I've created a view:
import UIKit
class TestNotWorkingView: UIView {
#IBOutlet weak var label1: UILabel!
#IBOutlet weak var label2: UILabel!
#IBOutlet weak var label3: UILabel!
}
And connected with the corresponding XIB:
Then, I've added the view to the UIViewController on the Storyboard:
class ViewController: UIViewController {
#IBOutlet weak var testNotWorkingView: TestNotWorkingView!
override func viewDidLoad() {
super.viewDidLoad()
print(testNotWorkingView)
print(testNotWorkingView.label1)
}
}
When I run the app, only the parent view appears. No labels are visible:
Parent view:
Optional(TestProject.TestNotWorkingView: 0x7fb165e04610; frame = (0
0; 414 818); autoresize = RM+BM; layer = CALayer: 0x600001f254e0)
Querying one of the labels
nil
I've already set the TestNotWorkingView as the view's class and file owner.
I assume since the views are not present on the storyboard, they're not being created and linked at runtime.
How can I use views created in XIBs inside a ViewController created in Storyboard? Is it possible at all?
Test Project that fully reproduces the issue
Using view designed in xib can be a bit tricky, you need to add this code to your TestNotWorkingView class:
override func awakeAfter(using aDecoder: NSCoder) -> Any? {
guard subviews.isEmpty else {
return self
}
return Bundle.main.loadNibNamed("TestNotWorkingView", owner: nil, options: nil)?.first
}
EDIT: also, you should leave File's Owner Empty, and connect outlets only to TestNotWorkingView

Using LOTAnimationView with Auto Layout in Storyboard

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

create IBOutlet / IBAction & connect it with UI in a custom class

In my ViewController, I have two UIImageView which have IBOutlet connection to UI :
class ViewController: UIViewController {
#IBOutlet weak var img1: UIImageView!
#IBOutlet weak var img2: UIImageView!
...
Now, I want to refactor my project to have a dedicated class ImageManager handles the images. So, the ViewController becomes like this :
class ViewController: UIViewController {
var imgMgr: ImageManager!
override func viewDidLoad() {
super.viewDidLoad()
imgMgr = ImageManager()
}
}
I want the ImageManager class to declare the two UIMageViews the same way as that in ViewController and make the IBOutlet connection with UI, but looks like it doesn't work in this way:
class ImageManager : NSObject {
#IBOutlet weak var img1: UIImageView!
#IBOutlet weak var img2: UIImageView!
...
}
Is there any way to make a custom class holding the IBOutlet & IBAction which connects to interface builder UI components (instead of doing it in ViewController)?
I don't think you should do this. You should just manage your images in your custom UIImage class using for example:
//inside the function awakFromNib
self.layer.cornerRadius = 5.0 //will make all the images' corner radius of elements assigned to this custom class rounded
self.clipsToBounds = true
Then you will drag the image outlets as usual to the view controller, but instead of setting its class to "UIImage", set it to the custom class name
#IBOutlet weak var someImg: CustomImgClass!
Make ImageManager subclass of UIView.
Create a new View under your ViewController and change it class name to ImageManager.
Drag Two UIImageViews to your ImageManager.
See the out Outlets of ImageManager View and connect them to the newly created View.
In your viewWillAppear just add the ImageManager as subview.

Adding spaces in front of text field via subclassing

So I am currently trying to add some space before my text in a text field. So when I type, it starts with four spaces instead of none. So I did some research and I came across something that seems really good (as indicated by all the votes), i.e., Create space at the beginning of a UITextField. One problem is that I do not know how to actually implement this in my other classes (assuming that's what the post is intending the reader to do).
This is what I think I'm supposed to do. I think I'm supposed to instantiate an object of that class and use the methods in the class to add spaces in front of my text field. But I don't actually know what that looks like in code. Could anyone give me an example of how to actually implement the code on this post? Create space at the beginning of a UITextField
Here is the code that I have so far:
import UIKit
class SignUpViewController: UIViewController {
#IBOutlet weak var facebookButton: UIButton!
#IBOutlet weak var googleplusButton: UIButton!
#IBOutlet weak var fullNameTextField: UITextField!
#IBOutlet weak var emailAddressTextField: UITextField!
#IBOutlet weak var passwordTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
facebookButton.layer.cornerRadius = 5
googleplusButton.layer.cornerRadius = 5
fullNameTextField.layer.borderColor = UIColor.lightGrayColor().CGColor
fullNameTextField.layer.borderWidth = 1.0
emailAddressTextField.layer.borderColor = UIColor.lightGrayColor().CGColor
emailAddressTextField.layer.borderWidth = 1.0
passwordTextField.layer.borderColor = UIColor.lightGrayColor().CGColor
passwordTextField.layer.borderWidth = 1.0
}
}
You need to create subclass of UITextField. Then add this code in its implementation
static CGFloat leftMargin = 10;
- (CGRect)textRectForBounds:(CGRect)bounds
{
bounds.origin.x += leftMargin;
bounds.size.width -= (leftMargin + 20);
return bounds;
}
- (CGRect)editingRectForBounds:(CGRect)bounds
{
bounds.origin.x += leftMargin;
bounds.size.width -= (leftMargin + 20);
return bounds;
}
After that, set custom class to your UITextField.
You should create a class from #ScareCrow answer. After that go to the storyboard and change the class of the UITextField. Such as:
After that create an IBOutlet of the textfield. That outlet will be instance of the TextField class.

Resources