简体   繁体   English

UIImageView的大小大于在Table View中设置的框架

[英]UIImageView size is bigger than the frame set inside Table View

I have a tableview, which has a header of profileView. 我有一个tableview,其中有profileView的标头。

lazy var tableView : UITableView = {
    let tv = UITableView()
    tv.delegate = self
    tv.translatesAutoresizingMaskIntoConstraints = false
    tv.tableHeaderView = self.profileView
    tv.dataSource = self
    tv.backgroundColor = .clear
    tv.register(ReviewTableViewCell.self, forCellReuseIdentifier: ReviewTableViewCell.reuseIdentifier)
    tv.separatorStyle = .none
    return tv
}()

lazy var profileView : UIView = {
    let view = UIView()
    view.translatesAutoresizingMaskIntoConstraints = false
    return view
}()

Inside the profileView/tableview header I have an image named avatarImage. 在profileView / tableview标头中,有一个名为avatarImage的图像。

var avatarImage:UIImageView = {
    let iv = UIImageView()
    iv.translatesAutoresizingMaskIntoConstraints = false
    iv.backgroundColor = .blue
    iv.layer.cornerRadius = 10.0
    iv.layer.borderColor = UIColor.white.cgColor
    iv.layer.borderWidth = 3.0
    iv.contentMode = UIViewContentMode.scaleAspectFit
    return iv
}()

override func viewWillLayoutSubviews() {
    setupViews()
    setupProfileView()

    if let imageURL = selectedStylist?.imageUrl {
        avatarImage.loadImageUsingCacheWithUrlString(imageURL)
    }
}

I give the avatarImage a frame 我给avatarImage一个框架

func setupViews() {

    view.addSubview(tableView)
    NSLayoutConstraint.activate([

        tableView.topAnchor.constraint(equalTo: self.view.topAnchor),
        tableView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
        tableView.leftAnchor.constraint(equalTo: self.view.leftAnchor),
        tableView.rightAnchor.constraint(equalTo: self.view.rightAnchor),
    ])

    profileView.addSubview(avatarImage)

    avatarImage.frame = CGRect(origin: CGPoint(x: view.frame.width/2 - 80/2 ,
                                               y: yPositionOfAvatarImage),
                               size: CGSize(width: 80,
                                            height: 80))
}

This is the extension used to load the image from the url to the imageView 这是用于将图像从url加载到imageView的扩展名

let imageCache = NSCache<NSString, UIImage>()

extension UIImageView {

func loadImageUsingCacheWithUrlString(_ urlString: String) {

    self.image = nil

    //check cache for image first
    if let cachedImage = imageCache.object(forKey: urlString as NSString){
        self.image = cachedImage
        return
    }

    //otherwise fire off a new download
    guard let url = URL(string: urlString) else {
        return
    }
    URLSession.shared.dataTask(with: url) {
        data, response, error in

        //download hit an error so lets return out
        if error != nil {

            return
        }
        DispatchQueue.main.async(execute: {
            if let downloadedImage = UIImage(data: data!) {
                imageCache.setObject(downloadedImage, forKey: urlString as NSString)
                self.image = downloadedImage
            }
        })
        }.resume()
   }

}

Instead of the image being 80x80 and fitting inside avatarImage, the avatarImage seems to fill the screen and, I believe, be the size of the image fetched rather than the size of the imageView. 图像不是80x80并适合放置在avatarImage内,而avatarImage似乎填满了屏幕,并且我相信是获取的图像的大小而不是imageView的大小。 If the image fetched is 1024*680, the size of the imageView will be 1024*680 rather than 80*80. 如果获取的图像为1024 * 680,则imageView的大小将为1024 * 680,而不是80 * 80。

I give the avatarImage a frame 我给avatarImage一个框架

But this line: 但是这一行:

iv.translatesAutoresizingMaskIntoConstraints = false

means "Ignore the frame I give this view! Use constraints instead." 表示“忽略我给出的视图框架!改为使用约束。” Well, using constraints the rule is that by default an image view is the size of its image. 好吧,使用约束的规则是默认情况下,图像视图是其图像的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UICollectionView的帧大小大于UIScreen的大小 - Frame size of UICollectionView is bigger than size of UIScreen 使UIButton imageView比框架更大,然后回到原来的尺寸? - make a UIButton imageView Bigger than the frame and then back to its original size? 为什么UIImageView的框架大于屏幕尺寸? - Why is my UIImageView's frame greater than the screen size? 在表格视图单元格内的 UIImageView 中设置图像 - Setting an image in a UIImageView inside a table view cell UIView帧大小没有改变UIImageView放在它里面 - UIView Frame Size Didn't Change UIImageView Placed inside it 导航栏按钮项大于我设置的框架 - navigation bar button item is bigger than the frame i set 为什么缩放比UIImageVIew尺寸更大? (使用swift) - Why scale to fill give bigger image than UIImageVIew size? (using swift) 在ViewController中实例化UIImageView时如何设置其大小/框架和contentMode? - How to set size/frame and contentMode of a UIImageView when its instantiated in the viewController? 在initWithCoder中设置UIImageView框架 - Set UIImageView frame in initWithCoder 在UIStackView()中使一个视图比其他视图更大 - Make one view bigger than others inside UIStackView()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM