Assign xib to the UIView in Swift

前端 未结 9 1938
死守一世寂寞
死守一世寂寞 2020-11-30 20:11

in objective c it can be done in init method by

-(id)init{
    self = [[[NSBundle mainBundle] loadNibNamed:@\"ViewBtnWishList\" owner:0 options:nil]     obje         


        
相关标签:
9条回答
  • 2020-11-30 20:36

    Tested in Xcode 7 beta 4 , Swift 2.0 . The following code will assign xib to the UIView. You can use this custom xib view in storyboard and access the IBOutlet object also.

    import UIKit
    
    @IBDesignable class SimpleCustomView:UIView
    {
        var view:UIView!;
    
        @IBOutlet weak var lblTitle: UILabel!
    
       @IBInspectable var lblTitleText : String?
            {
            get{
                return lblTitle.text;
            }
            set(lblTitleText)
            {
                lblTitle.text = lblTitleText!;
            }
        }
    
        override init(frame: CGRect) {
            super.init(frame: frame)
            loadViewFromNib ()
        }
    
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            loadViewFromNib ()
        }
        func loadViewFromNib() {
            let bundle = NSBundle(forClass: self.dynamicType)
            let nib = UINib(nibName: "SimpleCustomView", bundle: bundle)
            let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
            view.frame = bounds
            view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
            self.addSubview(view);
    
    
    
        }
    
    
    }
    

    Access customview programatically

    self.customView =  SimpleCustomView(frame: CGRectMake(100, 100, 200, 200))
            self.view.addSubview(self.customView!);
    

    Source code - https://github.com/karthikprabhuA/CustomXIBSwift

    0 讨论(0)
  • 2020-11-30 20:36

    that may be a solution for you:

    Swift 3.x

    class func instanceFromNib() -> UIView {
        return UINib(nibName: "<<NibFileName>>", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView
    }
    

    Swift 2.x

    class func instanceFromNib() -> UIView {
        return UINib(nibName: "<<NibFileName>>", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as UIView
    }
    
    0 讨论(0)
  • 2020-11-30 20:36

    instead of adding an extension to UIView, you could define a protocol and add the implementation to a protocol extension. You can then declare that UIView conforms to the protocol.

    This allows the return type to be Self instead of UIView. So the caller doesn't have to cast to the class.

    Explained here: https://stackoverflow.com/a/33424509/845027

    import UIKit
    
    protocol UIViewLoading {}
    extension UIView : UIViewLoading {}
    
    extension UIViewLoading where Self : UIView {
    
      // note that this method returns an instance of type `Self`, rather than UIView
      static func loadFromNib() -> Self {
        let nibName = "\(self)".characters.split{$0 == "."}.map(String.init).last!
        let nib = UINib(nibName: nibName, bundle: nil)
        return nib.instantiateWithOwner(self, options: nil).first as! Self
      }
    
    }
    
    0 讨论(0)
提交回复
热议问题