I have created a XIB for UIView named popUpView.xib and a swift file associated with it popUpView.swift
My popUpView.swift code is like this:
@objc class popUpView: UIView {
@IBOutlet weak var containerView: UIView!
@IBOutlet weak var mobileNumberTF: UITextField!
@IBOutlet weak var customerIdTF: UITextField!
@IBOutlet weak var panNumberTF: UITextField!
@IBOutlet weak var cancelBtn: UIButton!
@IBOutlet weak var saveBtn: UIButton!
override init(frame: CGRect) {
super.init(frame: frame)
self.initSubViews()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.initSubViews()
}
func initSubViews() {
let name = String(describing: type(of: self))
let nib = UINib(nibName: name, bundle: .main)
nib.instantiate(withOwner: self, options: nil)
self.addSubview(self.containerView)
self.containerView.translatesAutoresizingMaskIntoConstraints = false
self.addConstraints()
}
func addConstraints() {
NSLayoutConstraint.activate([
self.topAnchor.constraint(equalTo: containerView.topAnchor),
self.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
self.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
self.bottomAnchor.constraint(equalTo: containerView.bottomAnchor)
])
}
}
Now I am trying to load this view as a popUp in my ViewController written in Objective-C
In my ViewController, I am calling like this:
-(void)showPopUpView {
popUpView *popUpView = [[popUpView alloc] initWithFrame:CGRectZero];
[self.view addSubview:popUpView];
[popUpView.topAnchor constraintEqualToAnchor:self.view.topAnchor constant:25].active = YES;
[popUpView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:25].active = YES;
[popUpView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:25].active = YES;
[popUpView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor constant:25].active = YES;
}
In viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
[self showPopUpView];
}
I think I might be doing it the wrong way. What is the right way to load this custom view programatically in Obj-C?