qmathe / DropDownMenuKit

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

Title view collapses on the size of the first item. #10

Closed mario-deluna closed 7 years ago

mario-deluna commented 7 years ago

Hello, I've got an issue with the DropDownTitleView, it seems to fix it's size to the first assigned text.

Even when constructing the view with a way to large frame I run into the same issue:

DropDownTitleView(frame: CGRect(x: 0, y: 0, width: 400, height: 40))

Images say more than words:

Do you have a recommendation how to solve this issue?

And a big thank you for the work you have put into this UI component.

qmathe commented 7 years ago

Hi,

Sorry for the very late reply, I was away for a while. I'm unable to reproduce your issue. May be you are updating the title view label directly with DropDownTitleView.titleLabel.text instead of using DropDownTitleView.title?

As shown in ViewController of DropDownMenuExample, the title view label must be updated like this:

    @IBAction func choose(_ sender: AnyObject) {
        titleView.title = "a new title"
    }

It's important to use the title property to get the title view internal layout recomputed. This layout computation is triggered by DropDownTitleView.title setter:

    open var title: String? {
        get {
            return titleLabel.text
        }
        set {
            titleLabel.text = newValue

            titleLabel.sizeToFit()
            layoutSubviews()
            sizeToFit()
        }
    }

If this doesn't help, could you show me the code that sets up the drop down menu and the one that updates the title view?

vadimvitvickiy commented 7 years ago

You should change layout for label for something like this

titleLabel.frame.origin.x = frame.width/2 - titleLabel.frame.width/2

It's not about problem of TS, but about short titles

qmathe commented 7 years ago

Thanks for the suggestion, I'll commit a fix similar to this.

Did you manage to recreate the problem with the app example? Is it related to have left or right items in the navigation bar along the custom title view?

vadimvitvickiy commented 7 years ago

Did you manage to recreate the problem with the app example? Is it related to have left or right items in the navigation bar along the custom title view?

I had both left and right items in navigationBar If u mean theme subject, I don't have problem like this, only short titles wrong layout which was left aligned

qmathe commented 7 years ago

@pluckq Thanks for your additional explanations.

I just committted a fix that should solve this problem together with issue #15

Let me know if this fix works well.