0

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?
Unwind
  • 3
  • 2
  • I think you are missing the constraints for the popup view in ViewController. What exactly is the issue? – Arun Dec 16 '20 at 04:39
  • @Arun I have added the constraints, still view doesn't show up. I am calling it from viewDidLoad() method. – Unwind Dec 16 '20 at 06:07
  • Maybe add translatesAutoresizingMaskIntoConstraints = false for the popup view – Arun Dec 16 '20 at 07:20
  • 1
    This will help https://stackoverflow.com/questions/863321/how-to-load-a-uiview-using-a-nib-file-created-with-interface-builder – Arun Dec 16 '20 at 07:21
  • @Arun I was not able to load the view programatically. But now after I took a view in myViewController XIB and giving the view class as popUPView, I am able to show. This is not actually what I wanted but Its working fine for now.. Thanks for the help anyway. – Unwind Dec 17 '20 at 02:50
  • Please refer my answer – Arun Dec 17 '20 at 04:42

1 Answers1

0

I got curious since the last time I did this, so I created a project to test it out. This is how it works for me.

My xib: enter image description here

My PopupView.m enter image description here

My ViewController.m enter image description here

Added the code to this GitHub repo: https://github.com/ArunEA/ObjcViewWithNib

Arun
  • 160
  • 9