• iOS swift使用xib绘制UIView


    目标:用xib绘制一个UIView,在某个ViewController中调用。

    三个文件:ViewController.Swift    DemoView.swift     DemoView.xib


    首先,可以专心将DemoView.xib画出来,别忘记DemoView.xib中UIView的一处设置



    然后,写DemoView.swift文件,代码如下:

     

    1. class CoreView: UIView {  
    2.   
    3.     //MARK:  
    4.     //MARK: properties  
    5.      
    6.     @IBOutlet weak var makefriendsBtn: UIButton!  
    7.     @IBOutlet weak var networkBtn: UIButton!  
    8.     @IBOutlet weak var everyoneBtn: UIButton!  
    9.    
    10.     //MARK:  
    11.     //MARK: constraints  
    12.       
    13.     @IBOutlet weak var makefriendsBtnWidth: NSLayoutConstraint!  
    14.     @IBOutlet weak var networkBtnWidth: NSLayoutConstraint!  
    15.     @IBOutlet weak var everyoneBtnWidth: NSLayoutConstraint!  
    16.       
    17.       
    18.     //MARK:  
    19.     //MARK: functions  
    20.       
    21.     required init(coder aDecoder: NSCoder) {  
    22.         super.init(coder: aDecoder)  
    23.           
    24.     }  
    25.       
    26.       
    27.     // Only override drawRect: if you perform custom drawing.  
    28.     // An empty implementation adversely affects performance during animation.  
    29.     override func drawRect(rect: CGRect) {  
    30.         makeupUI()  
    31.     }  
    32.       
    33.      
    34.     func makeupUI() {  
    35.   
    36.         self.layer.masksToBounds = true  
    37.         self.layer.cornerRadius = 3  
    38.           
    39.         makefriendsBtn.layer.borderWidth = 1  
    40.         makefriendsBtn.layer.cornerRadius = 3  
    41.         makefriendsBtn.layer.borderColor = UIColor(red: 107/256, green: 167/256, blue: 249/256, alpha: 1).CGColor  
    42.         makefriendsBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Selected)  
    43.         makefriendsBtn.addTarget(self, action: "buttonSelected:", forControlEvents: UIControlEvents.TouchUpInside)  
    44.           
    45.         networkBtn.layer.borderWidth = 1  
    46.         networkBtn.layer.cornerRadius = 3  
    47.         networkBtn.layer.borderColor = UIColor(red: 107/256, green: 167/256, blue: 249/256, alpha: 1).CGColor  
    48.         networkBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Selected)  
    49.         networkBtn.addTarget(self, action: "buttonSelected:", forControlEvents: UIControlEvents.TouchUpInside)  
    50.           
    51.         everyoneBtn.layer.borderWidth = 1  
    52.         everyoneBtn.layer.cornerRadius = 0  
    53.         everyoneBtn.layer.borderColor = UIColor(red: 107/256, green: 167/256, blue: 249/256, alpha: 1).CGColor  
    54.         everyoneBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Selected)  
    55.         everyoneBtn.addTarget(self, action: "buttonSelected:", forControlEvents: UIControlEvents.TouchUpInside)  
    56.           
    57.           
    58.         makefriendsBtnWidth.constant = (self.frame.width - 32 - 29) / 3 + 10  
    59.         networkBtnWidth.constant = (self.frame.width - 32 - 29) / 3 + 2  
    60.         everyoneBtnWidth.constant = (self.frame.width - 32 - 29) / 3 - 2  
    61.           
    62.           
    63.     }  
    64.       
    65.       
    66.     func buttonSelected(button: UIButton) {  
    67.           
    68.         button.selected = !button.selected  
    69.           
    70.         if button.selected == true {  
    71.             button.backgroundColor = UIColor(red: 107/256, green: 167/256, blue: 249/256, alpha: 1)  
    72.         } else {  
    73.             button.backgroundColor = UIColor.whiteColor()  
    74.         }  
    75.     }  
    76.       
    77.       
    78. }  


    下面就可以在ViewController.swift中调用了:

      1. var myView = NSBundle.mainBundle().loadNibNamed("DemoView", owner: nil, options: nil).first as? DemoView  
      2.           
      3.         myView?.frame = CGRect(x: 0, y: 0,  self.view.frame.width-50, height: self.view.frame.height-140)  
      4.         myView?.center = self.view.center  
      5.           
      6.         if myView != nil {  
      7.             self.view.addSubview(myView!)  
      8.         } 
  • 相关阅读:
    第一册:lesson 117.
    第一册:lesson 115.
    Map集合。
    第一册:lesson 113.
    第一册:lesson 111.
    泛型。
    EXT.NET初学
    LINQ查询
    后台调用前端JS
    数字与数组或者字符串里面的内容比较
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/5607320.html
Copyright © 2020-2023  润新知