sochalewski / UIImageViewAlignedSwift

A UIImageView subclass which allows you to align the image left/right/top/bottom, even when contentMode is AspectFit. Swift now.
MIT License
236 stars 67 forks source link

Seems not to work? #14

Closed marbetschar closed 7 years ago

marbetschar commented 7 years ago

I've tried to integrate your UIImageViewAlignedSwift class as follows, but unfortunately the UIImage itself is not rendered at all (see attached screenshot). What's wrong?

let imageView = UIImageViewAligned(image: UIImage(named:"my-image"))
imageView.contentMode = .scaleAspectFit
imageView.scaling = .down
imageView.alignment = .bottomRight
imageView.backgroundColor = .red

imageView.translatesAutoresizingMaskIntoConstraints = false

self.view.addSubview(self.rightImageView)

let imageViews = ["imageView":imageView]

self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[imageView(32)]-6-|", options: [], metrics: nil, views: imageViews))
self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[imageView(32)]-6-|", options: [], metrics: nil, views: imageViews))

And here is the result (the red box in the bottom right corner):

simulator screen shot 21 02 2017 20 10 27

marbetschar commented 7 years ago

Any idea why it's not working as expected? Or am I doing something wrong?

marbetschar commented 7 years ago

just noticed it seems to work if I set the image after initialization of the imageView as follows:

imageView.image = UIImage(named: "my-image")

Seems like a bug.

marbetschar commented 7 years ago

For some reason this library does not compile for an archived app version when checked out via Carthage. FWIW: I've subclassed your as follows - this refuses to compile for the app store:

import UIKit
import UIImageViewAlignedSwift

class ChartImageView: UIImageViewAligned{

    var imageMaskingColorComponents: [CGFloat]?
    var imageTintColor: UIColor?

    override var image: UIImage?{
        get{
            return super.image
        }
        set{
            var newImage = newValue

            if let color = self.imageTintColor{
                newImage = newImage?.applied(tintColor: color)
            } else if let colorComponents = self.imageMaskingColorComponents{
                newImage = newImage?.masked(withColorComponents: colorComponents)
            }

            super.image = newImage
        }
    }
}
piotrros commented 7 years ago

I've just stepped into the same problem. Thanks for the hint. Seems like a bug.

sochalewski commented 7 years ago

@marbetschar @piotrros It's fixed since 0.4.0. In addition scaling is not available, as long as it doesn't work as desired. Not sure about Carthage, because I don't use it and my knowledge about this is really poor. Please PR if you know how to fix this 👍