2016-06-15 3 views
0

私はUITableViewCellのサブクラスをセル内に持っています。コンテンツモードがScaleAspectFitに設定されたUIImageViewがあります。 UIImageViewは静的な高さを持ち、等価な先導&の後続の制約を使用しますが、上限にも制約があります。UITableViewCellのUIImageViewの画像に基づいてUIViewの幅を設定します。

UIImageViewのすぐ下に、私はUIViewを持っていますが、その幅は拡大縮小された画像の幅と同じにしたいと思います。理想的には、最小幅も設定したいと思います。

イメージビューでイメージのサイズを取得するのに問題があるようです。

import UIKit 

class ViewController: UIViewController { 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 
} 

extension ViewController: UITableViewDataSource, UITableViewDelegate { 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 1 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    guard let cell = tableView.dequeueReusableCellWithIdentifier("sampleCell") as? SampleCellTableViewCell else { 

     return UITableViewCell() 
    } 

    return cell 
} 
} 

持っていれば、私は新鮮なアイデアをしたいと思いますので、この点に多くのことを試してみた

class SampleCellTableViewCell: UITableViewCell { 

@IBOutlet weak var theImage: UIImageView! 
@IBOutlet weak var theView: UIView! 

override func awakeFromNib() { 
    super.awakeFromNib() 
    // Initialization code 
} 

override func setSelected(selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated: animated) 

    // Configure the view for the selected state 
} 
} 

enter image description here

//セルは、ありがとうございました。

+0

? – Ike10

答えて

0

解決策は、ビューに幅の制約を追加し、それに対するコンセントを作成し、その定数値を画像ビュー内の拡大画像の幅に変更することになりました。

//セル

class SampleCellTableViewCell: UITableViewCell { 

@IBOutlet weak var theImage: UIImageView! 
@IBOutlet weak var theView: UIView! 
@IBOutlet weak var theViewWidthConstraint: NSLayoutConstraint! 

override func awakeFromNib() { 
    super.awakeFromNib() 
    // Initialization code 
} 

override func setSelected(selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated: animated) 

    // Configure the view for the selected state 
} 
} 

// cellForRowAtIndexPath

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    guard let cell = tableView.dequeueReusableCellWithIdentifier("sampleCell") as? SampleCellTableViewCell else { 

     return UITableViewCell() 
    } 

    cell.theViewWidthConstraint.constant = cell.theImage.frameForImageInImageViewAspectFit().width 

    return cell 
} 

あなたがここにframeForImageInImageViewAspectFit()メソッドの定義を見つけることができます:https://stackoverflow.com/a/36428745/4096655

enter image description here

特にあなたが試してみました何
関連する問題