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

Margins of header/footer cannot be set using .onSetupView #452

Closed
plastus opened this issue May 25, 2016 · 3 comments
Closed

Margins of header/footer cannot be set using .onSetupView #452

plastus opened this issue May 25, 2016 · 3 comments

Comments

@plastus
Copy link

plastus commented May 25, 2016

The following code has no effect on the layout margins of the default header/footer method.

            +++ Section { s in
                var footer = HeaderFooterView<UITableViewHeaderFooterView>(.Class)
                footer.onSetupView = { view,_,_ in
                    view.preservesSuperviewLayoutMargins = false
                    view.contentView.preservesSuperviewLayoutMargins = false
                    view.contentView.layoutMargins.left = 50
                    view.layoutMargins.left = 50
                    view.textLabel!.text = "Testing a really long string to make sure that it wraps around properly."
                    view.textLabel!.preservesSuperviewLayoutMargins = false
                    view.textLabel!.layoutMargins.left = 50
                }
                s.footer = footer
            }
  1. How can the left/right margins be set properly in this scenario?

  2. Is the proper way of adding images to a header view using custom class? What if I have 5 sections and want to use different images for each one? Is there a way to just pass the name of the image to be used instead of re-writing the same code over and over again under different class names?

  3. How to manipulate label strings to eliminate all character capitalization?

    Section(header: "Application Settings", footer: "")
    

For instance, with the above simple section definition....how would I accomplish 1,2,3?

Would any of this require overriding built-in tableView functions? I've been trying to discover the answer but so far I've hit dead-ends in potential solutions.

As always, help/hints are greatly appreciated!

@plastus
Copy link
Author

plastus commented May 25, 2016

Ok, I've come up with the following:

class GenericFooter: UIView {

override init(frame: CGRect) {
    super.init(frame: frame)
    self.preservesSuperviewLayoutMargins = false
    let textView = UILabel()
    textView.frame = CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width - 30, height: 20 )
    textView.text = "This is  a very very very long title, This is  a very very very long title, This is  a very very very long title, This is  a very very very long title, This is  a very very very long title, This is  a very very very long title Let's add some more stuff here to see how it handles."
    textView.numberOfLines = 0
    textView.textAlignment = .Justified
    textView.font = UIFont.preferredFontForTextStyle(UIFontTextStyleFootnote)
    textView.textColor = UIColor(red:0.47, green:0.47, blue:0.49, alpha:1.0)
    textView.frame = textView.bounds
    textView.sizeToFit()
    self.addSubview(textView)
    self.bounds = CGRect(x: -15, y: -3, width: textView.bounds.width - 16, height: textView.bounds.height)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
}

My question now: How can I pass along the string that would set textView.text?

To set the section I do:

    +++ Section() {
        $0.header = HeaderFooterView<GenericHeader>(HeaderFooterProvider.Class)
        $0.footer = HeaderFooterView<GenericFooter>(HeaderFooterProvider.Class)
    }

@plastus
Copy link
Author

plastus commented May 26, 2016

I've tried the following with no luck. label is always nil, even though I define it in '.onSetupView'

class GenericHeader2: UIView {

    lazy var label: String = {
        var somestring = String()
        return somestring
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        var textView = UILabel()
        textView.frame = CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width - 30, height: 20 )
        textView.text = label
        textView.numberOfLines = 0
        textView.textAlignment = .Justified
        textView.font = UIFont.preferredFontForTextStyle(UIFontTextStyleBody)
        textView.textColor = UIColor(red:0.47, green:0.47, blue:0.49, alpha:1.0)
        textView.frame = textView.bounds
        textView.sizeToFit()
        self.preservesSuperviewLayoutMargins = false
        self.addSubview(textView)
        self.bounds = CGRect(x: -15, y: -3, width: textView.bounds.width - 15, height: textView.bounds.height + 8)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

Attempting to set variable for title string.

+++ Section() { s in
    var header = HeaderFooterView<GenericHeader2>(HeaderFooterProvider.Class)
    var footer = HeaderFooterView<GenericFooter>(HeaderFooterProvider.Class)
    header.onSetupView = { v,s in
        v.label = "Some very very very long string will go here so that we can see whether this class performs the drawing correctly."
    }
    s.header = header
    s.footer = footer
}

Edit: Is there a reason why .onSetupView is called multiple times? I was doing some de-bugging and it looks like a print statement there is executed between 5-15 times....

@mats-claassen
Copy link
Member

It should be called once every time the tableView reloads that section.
You have to consider that init is called before onSetupView so that label will always be empty. You should set the text of your label in onSetupView.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants