Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a custom UIView with intrinsic content height #58

Closed
eliburke opened this issue Feb 8, 2018 · 2 comments
Closed

Adding a custom UIView with intrinsic content height #58

eliburke opened this issue Feb 8, 2018 · 2 comments
Labels
Projects

Comments

@eliburke
Copy link
Contributor

eliburke commented Feb 8, 2018

Yesterday, I needed to add an activity spinner to one of my bulletin board pages, but I wanted it inline instead of a full page spinner. It is essentially: "Downloading Images" [spinner] [cancel button]

A standard UIView can't be easily added to the contentViews stack because it has no intrinsic content size. If you don't set a frame, it is sized to 0x0. If you do set a frame, it must be manually centered without access to the superview's frame. And you can't use constraints because it hasn't been added to the superview.

This custom UIView (thanks StackOverflow!) solves the problem by forcing an intrinsic size.

// NOTE: only height is honored by the current StackView settings
class IntrinsicSizeView: UIView {
    var width: CGFloat = 32.0
    var height: CGFloat = 32.0
    convenience init(width: CGFloat, height: CGFloat) {
        self.init()
        self.width = width
        self.height = height
    }
    override var intrinsicContentSize: CGSize {
        get {
            return CGSize(width: width, height: height)
        }
    }
}

Now as it turns out, UIActivityIndicatorView does have an intrinsic size, and this wasn't necessary for my particular case. But it may help someone else, and I thought it might be a nice addition to BulletinInterfaceBuilder, e.g. makeFixedSizeView(width, height) could vend a BulletinBoardFixedHeightView or something along those lines.

@alexisakers alexisakers added this to To do in Release V2 Apr 29, 2018
@alexisakers alexisakers moved this from To do to Done in Release V2 May 25, 2018
@alexisakers
Copy link
Owner

Thanks for the suggestion! I've implemented this in 933402c. There is now a view named BLTNContainerView that allows you to specify a content size. You can set the child view (such as an activity indicator or a collection view), and set up its constraints with Auto Layout inside a block.

As you suggested, I added a utility method on BLTNInterfaceBuilder to make integration easier. For example, if you had an activity indicator, you could add it to the stack this way:

override func makeViewsUnderDescription(with interfaceBuilder: BLTNInterfaceBuilder) -> [UIView]? {
    let activityIndicator = UIActivityIndicatorView()
    let container = interfaceBuilder.wrapView(activityIndicator, width: 44, height: 44, position: .centered)
    return [container]
}

You can pass centered or pinnedToEdges as the position, depending on your needs.

Do not hesitate to provide feedback!

@alexisakers
Copy link
Owner

This feature shipped with the version 2.0.0-beta.3 of the library. Thanks again for your suggestion. Do not hesitate to reopen the issue if you have any feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
No open projects
Release V2
  
Done
Development

No branches or pull requests

2 participants