Orderella / PopupDialog

A simple, customizable popup dialog for iOS written in Swift. Replaces UIAlertController alert style.

Home Page:http://www.mwfire.de

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Remove messageLabel and titleLabel

saidyuce opened this issue · comments

Hi, thanks for the library, it's awesome! I was wondering if there is a way to remove both the messageLabel and titleLabel from the DefaultView? I'm not familiar with the programmatic constraint stuff. I don't mind having title and message in the constructor, just a simple solution to remove those areas from the view would be fine. Also as a feedback, I think it would be cool to have options to disable certain areas of the popup. Thanks.

Hey there! The default view controller is just a convenience view that resembles UIAlertController layout. For custom layouts, you can always pass a custom view controller. Moreover, title and message are optional and should take zero space if not provided.

Still, for custom layouts custom view controllers are the way to go.

Thanks for the quick reply. I may go with the custom view controller approach then.

However, I didn't feel like title and message are optional since I tried passing in both "" and nil for them in the constructor and still they occupied empty space. I'm attaching a screenshot for title = nil and message = nil. Not an issue anymore as long as I can use a custom view controller, but just to let you know. Or am I missing something?

let popup = PopupDialog(title: nil, message: nil, image: UIImage(contentsOfFile: data[indexPath.row])!)

img_4660

Unfortunately, that's feature is not presented at this time. Yes, you could pass a nil values as text, but there are constraints between imageview, title and message.

constraints += NSLayoutConstraint.constraints(withVisualFormat: "V:|[imageView]-(==30@900)-[titleLabel]-(==8@900)-[messageLabel]-(==30@900)-|", options: [], metrics: nil, views: views)

For achieving that result, your should reassemble those (30, 8, 30) constraints with zero values (modify source code).

commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

I kinda got it to do what was needed with this code... I know it's not elegant... But it works.

In PopupDialogDefaultView.swift below the inits

    internal func setupViews(){
        translatesAutoresizingMaskIntoConstraints = false
        addSubview(imageView)
        addSubview(titleLabel)
        addSubview(messageLabel)
        setupConstraints()
    }

    var constToRemove: [NSLayoutConstraint] = [NSLayoutConstraint]()

    func setupConstraints(){
        if self.constToRemove.count > 0 {
            NSLayoutConstraint.deactivate(self.constToRemove)
            self.constToRemove.removeAll()
        }
        let views: [String: Any] = ["imageView": imageView, "titleLabel": titleLabel, "messageLabel": messageLabel] as [String: Any]
        self.constToRemove += NSLayoutConstraint.constraints(withVisualFormat: "H:|[imageView]|", options: [], metrics: nil, views: views)
        self.constToRemove += NSLayoutConstraint.constraints(withVisualFormat: "H:|-(==20@900)-[titleLabel]-(==20@900)-|", options: [], metrics: nil, views: views)
        self.constToRemove += NSLayoutConstraint.constraints(withVisualFormat: "H:|-(==20@900)-[messageLabel]-(==20@900)-|", options: [], metrics: nil, views: views)
        var const: String
        if self.titleLabel.text == nil {
            if self.messageLabel.text == nil {
                const = "V:|[imageView]-(==0@900)-[titleLabel]-(==0@900)-[messageLabel]-(==0@900)-|"
            } else {
                const = "V:|[imageView]-(==0@900)-[titleLabel]-(==25@900)-[messageLabel]-(==25@900)-|"
            }
        } else {
            if self.messageLabel.text == nil {
                const = "V:|[imageView]-(==25@900)-[titleLabel]-(==25@900)-[messageLabel]-(==0@900)-|"
            } else {
                const = "V:|[imageView]-(==30@900)-[titleLabel]-(==8@900)-[messageLabel]-(==30@900)-|"
            }
        }
        self.constToRemove += NSLayoutConstraint.constraints(withVisualFormat: const, options: [], metrics: nil, views: views)
        imageHeightConstraint = NSLayoutConstraint(item: imageView, attribute: .height, relatedBy: .equal, toItem: imageView, attribute: .height, multiplier: 0, constant: 0)
        if let imageHeightConstraint: NSLayoutConstraint = imageHeightConstraint {
            self.constToRemove.append(imageHeightConstraint)
        }
        NSLayoutConstraint.activate(self.constToRemove)
    }

And this in the image, title & message setters on PopupDialogDefaultViewController.swift

    standardView.setupConstraints()

Hope it helps someone out :)