美文网首页iOS-Developer-SwiftiOS开发
Swift中UIView快速修改frame

Swift中UIView快速修改frame

作者: 文刂Rn | 来源:发表于2016-10-10 11:09 被阅读772次

创建一个UIView的分类

import UIKit

extension UIView {
    
    public var ld_x: CGFloat {
        
        get {
            return self.frame.origin.x
        }
        
        set(newVal) {
            var ld_frame: CGRect = self.frame
            ld_frame.origin.x = newVal
            self.frame = ld_frame
            
        }
        
    }
    
    
    public var ld_y: CGFloat {
        
        get {
            return self.frame.origin.y
        }
        
        set(newVal) {
            var ld_frame: CGRect = self.frame
            ld_frame.origin.y = newVal
            self.frame = ld_frame
            
        }
        
    }
    
    public var ld_width: CGFloat {
        
        get {
            return self.frame.size.width
        }
        
        set(newVal) {
            var ld_frame: CGRect = self.frame
            ld_frame.size.width = newVal
            self.frame = ld_frame
            
        }
        
    }
    
    public var ld_height: CGFloat {
        
        get {
            return self.frame.size.height
        }
        
        set(newVal) {
            var ld_frame: CGRect = self.frame
            ld_frame.size.height = newVal
            self.frame = ld_frame
            
        }
        
    }
    
    public var ld_size: CGSize {
        
        get {
            return self.frame.size
        }
        
        set(newVal) {
            var ld_frame: CGRect = self.frame
            ld_frame.size = newVal
            self.frame = ld_frame
            
        }
        
    }
    
    public var ld_centerX: CGFloat {
        
        get {
            return self.center.x
        }
        
        set(newVal) {
            var ld_center: CGPoint = self.center
            ld_center.x = newVal
            self.center = ld_center
            
        }
        
    }
    
    public var ld_centerY: CGFloat {
        
        get {
            return self.center.y
        }
        
        set(newVal) {
            var ld_center: CGPoint = self.center
            ld_center.y = newVal
            self.center = ld_center
            
        }
        
    }
    
}

用法

 let view: UIView = UIView()
view.ld_x = 10
 view.ld_y = 10

相关文章

网友评论

    本文标题:Swift中UIView快速修改frame

    本文链接:https://www.haomeiwen.com/subject/jggmyttx.html