簡體   English   中英

Swift-從程序設計約束中拉伸UIImageView

[英]Swift - Stretched UIImageView from programmatically constraints

class Cell:UITableViewCell {
   var myImage:UIImageView = UIImageView()
   override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
        myImage.setTranslatesAutoresizingMaskIntoConstraints(false)
        contentView.addSubview(userImage)
        let viewsDict = ["contentView":contentView,"myImage":myImage]
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[contentView(100)]", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-30-[myImage]", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[myImage]-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
   }
}

這是我的代碼,我想將myImage居中放置-垂直放置,但是當我添加V:|-[myImage]-| 它被拉長了。 我已經嘗試使用V:|-[myImage(50)]-| ,但它沒有垂直居中,並且收到錯誤消息:

2015-01-12 17:02:37.988 myApp[17463:1493388] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x7a197470 V:[UITableViewCellContentView:0x7a1953d0(100)]>",
    "<NSLayoutConstraint:0x7a1982f0 UIImageView:0x7a194bc0.top == UITableViewCellContentView:0x7a1953d0.topMargin>",
    "<NSLayoutConstraint:0x7a198360 V:[UIImageView:0x7a194bc0(50)]>",
    "<NSLayoutConstraint:0x7a198320 UITableViewCellContentView:0x7a1953d0.bottomMargin == UIImageView:0x7a194bc0.bottom>"
)

我已經myImage.clipsToBounds = true在Cell類和func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)使用myImage.clipsToBounds = true ,但這並不影響拉伸...需要幫助..:D

您可以做的一件事是傳入度量字典:

 let metrics = ["imageWidth":self.imageView.frame.width, "borderSpacing": 30]

然后將其傳遞給約束:

contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[myImage(imageWidth)]-|", options: NSLayoutFormatOptions(0), metrics: metrics, views: viewsDict))

接下來,您想要將此優先級設置為必填(1000),然后將其他優先級設置為<1000

contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-(>=borderSpacing@999)-[myImage(imageWidth@1000)]-(>=borderSpacing@999)-|", options: NSLayoutFormatOptions(0), metrics: metrics, views: viewsDict))

這都是概念上的。 我沒有測試此代碼。 祝好運。

嘗試一下-我很難弄清楚是什么約束導致了此錯誤。 這是一種更簡單的方法。

我正在使用Xcode 6.1.1

“命令+ A”選擇所有UILabel,UIImage等。單擊編輯器->固定>(選擇...)以再次查看,單擊編輯器->解決自動布局問題->添加缺少的約束或重置為建議的約束。 這取決於您的情況。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM