qmathe / DropDownMenuKit

UIKit drop down menu, simple yet flexible and written in Swift
Other
303 stars 38 forks source link

Add customisation #8

Closed nicholas-jomo closed 7 years ago

nicholas-jomo commented 7 years ago

This library is great, I've added a few configuration so it can fit into more design.

cheers.

qmathe commented 7 years ago

Thanks! Looks like you added many handy features. I'll take a more in-depth look at your pull request in the next days.

qmathe commented 7 years ago

I just took a look at your pull request. The changes in DropDownMenu and DropDownMenuCell looks good.

For DropDownTitleView, to support customizing the up/down images, I'd suggest to simply declare iconSize as an instance variable rather than a static one, and change setUp() into an open method:

open var iconSize = CGSize(width: 12, height: 12)

open func setUp() { // some code }

This also means replacing any access to DropDownTitleView.iconSize with self.iconSize.

Then it's possible to customize the images or image views with a subclass like this:

open class CustomDropDownTitleView : DropDownTitleView {

    open override func setUp() {
        iconSize = CGSize(width: 8, height: 8)
        menuDownImageView.image = UIImage(named: "Ionicons-ios-checkmark-outline")
        menuDownImageView.transform = CGAffineTransform.identity
        menuUpImageView.image = UIImage(named: "Ionicons-ios-search")
        super.setUp()
    }
}

You could also customize properties like iconSize or menuDown/UpImageView from the outside without creating a dedicated subclass.

Let me know if this approach would work for you.

nicholas-jomo commented 7 years ago

Thanks for your suggestion!

I'm not sure if my approach is the best, my thoughts are:

Thoughts are appreciated.

qmathe commented 7 years ago

For iconSize, I want to keep it, since it makes it easy to scale images or use images with mismatched sizes. If you want a more precise control on the image view positions and sizes, you can override layoutSubviews().

To get the image views properly resized when setting their images, the best would be to trigger a new layout update when setting the icon size and recompute the image view sizes in layoutSubviews().

For the API, I want to avoid introducing a new class, struct or methods like DropDownTitleConfig. The behavior you're looking for can be supported by tweaking the implementation I think.

Here is a revised version of the code I gave in my previous comment. The following should be extracted out of menuDown/UpImageView initialization code and put into layoutSubviews():

        menuDownImageView.frame.size = iconSize
        menuUpImageView.frame.size = iconSize
        imageView.frame.size = iconSize

The property iconSize should be changed to:

open var iconSize = CGSize(width: 12, height: 12) {
        didSet {
             setNeedsLayout()
        }
}

With these changes, you can now easily update the images without even overriding setUp(). For instance, the following can be put in ViewController.select() of the app example:

        titleView.iconSize = CGSize(width: 24, height: 24)
        titleView.menuDownImageView.image = UIImage(named: "Ionicons-ios-checkmark-outline")
        titleView.menuDownImageView.transform = CGAffineTransform.identity
        titleView.menuUpImageView.image = UIImage(named: "Ionicons-ios-search")
nicholas-jomo commented 7 years ago

thanks again. I tried your approach and it all worked out. Added example codes too

qmathe commented 7 years ago

I had completely forgot to merge this pull request, sorry.

The changes are now merged into master. I fixed issues related to indentation and height support.

Thanks a lot for your contribution.