SwiftKickMobile / SwiftMessages

A very flexible message bar for UIKit and SwiftUI.
MIT License
7.33k stars 747 forks source link

Custom width on a UINavigationController segue programmatically #313

Closed n3d1117 closed 5 years ago

n3d1117 commented 5 years ago

Hello, awesome library!

I'm trying to use SwiftMessagesSegue with a UITableViewController inside a UINavigationController using .centered layout. So far works fine:

let nav = UINavigationController(rootViewController: MyTableViewController())
let segue = SwiftMessagesSegue(identifier: nil, source: self, destination: nav)
segue.configure(layout: .centered)
segue.messageView.backgroundHeight = 250
segue.perform()
class MyTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(dismissAnimated))

        // other tableview stuff
    }

    @objc fileprivate func dismissAnimated() {
        dismiss(animated: true)
    }

}

ss1

However I noticed that the modal goes full width even in landscape and on iPads, making it look way too stretched. Is there any way I can set a max width?

ss2 ss3

Thanks!

wtmoose commented 5 years ago

I'm working on better defaults for iPad width, which should be available within the next week or so.

As a quick fix, you could try adding a max width constraint to segue.messageView.backgroundView.

n3d1117 commented 5 years ago

Hello, Thanks for the quick reply! I tried the fix you mentioned:

segue.messageView.backgroundView.widthAnchor.constraint(lessThanOrEqualToConstant: 600).isActive = true

But it conflicts with other constraints:

la

wtmoose commented 5 years ago

Can you try the head of master and let me know if it works for you? It has improved default sizing on iPad and fixes the Auto Layout error you had. Here are the updated readme notes on sizing:

Sizing

SwiftMessagesSegue provides static default view controller sizing based on device. However, it is recommended that you specify sizing appropriate for your content using one of the following methods.

  1. Define sufficient width and height constraints in your view controller.
  2. Set preferredContentSize (a.k.a "Use Preferred Explicit Size" in Interface Builder's attribute inspector). Zeros are ignored, e.g. CGSize(width: 0, height: 350) only affects the height.
  3. Add explicit width and/or height constraints to segue.messageView.backgroundView.

Note that Layout.topMessage and Layout.bottomMessage are always full screen width. For other layouts, the there is >a maximum 500pt width on iPad (regular horizontal size class) at 950 priority, which can be overridden by adding higher-priority constraints.

n3d1117 commented 5 years ago

Just tested, it's working now @wtmoose! Great addition to the library, I think you can close this. Thank you!

wtmoose commented 5 years ago

Fixed in 7.0.0.