你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS swift使用xib繪制UIView

iOS swift使用xib繪制UIView

編輯:IOS開發綜合

目標:用xib繪制一個UIView,在某個ViewController中調用。

三個文件:ViewController.swift DemoView.swift DemoView.xib

 

首先,可以專心將DemoView.xib畫出來,別忘記DemoView.xib中UIView的一處設置

\

 

然後,寫DemoView.swift文件,代碼如下:

 

class CoreView: UIView {

    //MARK:
    //MARK: properties
   
    @IBOutlet weak var makefriendsBtn: UIButton!
    @IBOutlet weak var networkBtn: UIButton!
    @IBOutlet weak var everyoneBtn: UIButton!
 
    //MARK:
    //MARK: constraints
    
    @IBOutlet weak var makefriendsBtnWidth: NSLayoutConstraint!
    @IBOutlet weak var networkBtnWidth: NSLayoutConstraint!
    @IBOutlet weak var everyoneBtnWidth: NSLayoutConstraint!
    
    
    //MARK:
    //MARK: functions
    
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
    }
    
    
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        makeupUI()
    }
    
   
    func makeupUI() {

        self.layer.masksToBounds = true
        self.layer.cornerRadius = 3
        
        makefriendsBtn.layer.borderWidth = 1
        makefriendsBtn.layer.cornerRadius = 3
        makefriendsBtn.layer.borderColor = UIColor(red: 107/256, green: 167/256, blue: 249/256, alpha: 1).CGColor
        makefriendsBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Selected)
        makefriendsBtn.addTarget(self, action: buttonSelected:, forControlEvents: UIControlEvents.TouchUpInside)
        
        networkBtn.layer.borderWidth = 1
        networkBtn.layer.cornerRadius = 3
        networkBtn.layer.borderColor = UIColor(red: 107/256, green: 167/256, blue: 249/256, alpha: 1).CGColor
        networkBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Selected)
        networkBtn.addTarget(self, action: buttonSelected:, forControlEvents: UIControlEvents.TouchUpInside)
        
        everyoneBtn.layer.borderWidth = 1
        everyoneBtn.layer.cornerRadius = 0
        everyoneBtn.layer.borderColor = UIColor(red: 107/256, green: 167/256, blue: 249/256, alpha: 1).CGColor
        everyoneBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Selected)
        everyoneBtn.addTarget(self, action: buttonSelected:, forControlEvents: UIControlEvents.TouchUpInside)
        
        
        makefriendsBtnWidth.constant = (self.frame.width - 32 - 29) / 3 + 10
        networkBtnWidth.constant = (self.frame.width - 32 - 29) / 3 + 2
        everyoneBtnWidth.constant = (self.frame.width - 32 - 29) / 3 - 2
        
        
    }
    
    
    func buttonSelected(button: UIButton) {
        
        button.selected = !button.selected
        
        if button.selected == true {
            button.backgroundColor = UIColor(red: 107/256, green: 167/256, blue: 249/256, alpha: 1)
        } else {
            button.backgroundColor = UIColor.whiteColor()
        }
    }
    
    
}

 

 

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

 

var myView = NSBundle.mainBundle().loadNibNamed(DemoView, owner: nil, options: nil).first as? DemoView
        
        myView?.frame = CGRect(x: 0, y: 0, width: self.view.frame.width-50, height: self.view.frame.height-140)
        myView?.center = self.view.center
        
        if myView != nil {
            self.view.addSubview(myView!)
        }

---------- over ----------

 

 
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved