最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[记录]尝试使用Cartography去实现自动布局

Swift crifan 2184浏览 0评论

折腾:

[已解决]swift中计算当前长按的坐标是属于自定义的UITableViewCell中哪个子视图的

中,frame不正常,width和height都是0

所以,打算好好去研究自动布局,以便解决布局问题

使得width和height是正常的

并且可以自动实现布局

不需要之前写了那么多的,那么乱的,各种计算大小的,代码了。

不错的文档:

robb/Cartography · GitHub

Cartography-Swift的自动布局第三方库(官方文档翻译) – Joe Shao in iOS – SegmentFault

去下载:

robb/Cartography · GitHub

https://github.com/robb/Cartography/archive/master.zip

最后,用:

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let curFileItem:FileItem = self.fileSectionList[indexPath.section].fileItemList[indexPath.row]
        let cell = FileTableViewCell(fileItem: curFileItem)
        return cell
    }
import UIKit
class FileTableViewCell: UITableViewCell {
    let curFileItem:FileItem
   
    var senderLabel:UILabel
    var timestampLabel:UILabel
    var fileIconImageView:UIImageView
    var filenameLabel:UILabel
    var sourceLabel:UILabel
    var sizeInKBLabel:UILabel
    var favoriateImageView:UIImageView
    init(fileItem:FileItem){
        self.curFileItem = fileItem
        self.senderLabel = UILabel()
        self.timestampLabel = UILabel()
        self.fileIconImageView = UIImageView()
        self.filenameLabel = UILabel()
        self.sourceLabel = UILabel()
        self.sizeInKBLabel = UILabel()
        self.favoriateImageView = UIImageView()
        super.init(style: UITableViewCellStyle.Subtitle, reuseIdentifier: StrIdFileTableViewCell)
        
        self.selectionStyle = UITableViewCellSelectionStyle.None
       
        self.accessoryType = UITableViewCellAccessoryType.None
        self.accessoryView = nil;
       
        self.addSubview(self.senderLabel)
        self.addSubview(self.timestampLabel)
        self.addSubview(self.fileIconImageView)
        self.addSubview(self.filenameLabel)
        self.addSubview(self.sourceLabel)
        self.addSubview(self.sizeInKBLabel)
        self.addSubview(self.favoriateImageView)
       
        self.textLabel?.text = curFileItem.name
        self.textLabel?.font = UIFont.systemFontOfSize(15)
        
        self.detailTextLabel?.text = "来自于:" + "某某某讨论组" + curFileItem.fromSource
        let sizeInKBFloat:Float = Float(curFileItem.sizeInBytes) / Float(1024)
        let sizeInKBStr:String = String(format: "%.2f", sizeInKBFloat) + "KB"
        self.sizeInKBLabel.text = sizeInKBStr
        self.sizeInKBLabel.font = UIFont.systemFontOfSize(13)
        self.imageView?.image = curFileItem.iconImage
        self.senderLabel.text = curFileItem.sender
        self.senderLabel.font = UIFont.systemFontOfSize(14)
        self.timestampLabel.text = curFileItem.updatedTime.toString("yy/MM/dd HH:mm:ss")
        self.timestampLabel.textColor = UIColor.grayColor()
        self.timestampLabel.font = UIFont.systemFontOfSize(13)
        self.timestampLabel.textAlignment = NSTextAlignment.Right
       
        constrain(detailTextLabel!, textLabel!) { detailTextLabel, textLabel in
            detailTextLabel.top == textLabel.bottom + 10
            detailTextLabel.left == textLabel.left
//            align(left: detailTextLabel, textLabel)
        }
       
        constrain(textLabel!, imageView!) { textLabel, imageView in
//            align(top: textLabel, imageView)
            textLabel.top == imageView.top
            textLabel.left == imageView.right + 10
        }
        constrain(senderLabel, imageView!) { senderLabel, imageView in
//            align(left: senderLabel, imageView)
            senderLabel.left == imageView.left
        }
       
        constrain(senderLabel) { senderLabel in
            senderLabel.top == senderLabel.superview!.top
//            senderLabel.left == senderLabel.superview!.left
        }
       
        constrain(sizeInKBLabel, detailTextLabel!) { sizeInKBLabel, detailTextLabel in
            sizeInKBLabel.left >= detailTextLabel.right + 40
            sizeInKBLabel.centerY == detailTextLabel.centerY
        }
       
        constrain(timestampLabel, sourceLabel) { timestampLabel, sourceLabel in
            timestampLabel.right == timestampLabel.superview!.right
//            timestampLabel.centerY == sourceLabel.centerY
            timestampLabel.top == sourceLabel.top
        }
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

实现了自动布局,效果很不错:

转载请注明:在路上 » [记录]尝试使用Cartography去实现自动布局

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
91 queries in 0.184 seconds, using 22.41MB memory