MoZhouqi / KMPlaceholderTextView

A UITextView subclass that adds support for multiline placeholder written in Swift.
MIT License
805 stars 141 forks source link

Fixes AutoLayout #13

Closed k3zi closed 8 years ago

k3zi commented 8 years ago

If the priority of the width constraint isn't reduced it is automatically broken. Running this without the priority lowered generates a string of warnings in the console log, meaning that this is being over constrained.

MoZhouqi commented 8 years ago

Hi, what is content of the warning message and when will the debug console output it?

k3zi commented 8 years ago
Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x14f664a00 h=--& v=--& H:[KMPlaceholderTextView.KMPlaceholderTextView:0x150069c00(0)]>",
    "<NSLayoutConstraint:0x14f780e40 UILabel:0x14f664340'Stream Description...'.width == KMPlaceholderTextView.KMPlaceholderTextView:0x150069c00.width - 40>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x14f780e40 UILabel:0x14f664340'Stream Description...'.width == KMPlaceholderTextView.KMPlaceholderTextView:0x150069c00.width - 40>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

Once I lower the priority of the constraint it breaks then it stops showing these warnings. The label "Stream Description..." in the logs is the text from the placeholder. This warning is output when the view and its subviews layout on the screen.

MoZhouqi commented 8 years ago

I suggest you to use auto layout instead of autoresizing mask to position KMPlaceholderTextView, you could set the translatesAutoresizingMaskIntoConstraints property of KMPlaceholderTextView to NO when using auto layout.

k3zi commented 8 years ago

I'm already using PureLayout which sets that property to false by default.

MoZhouqi commented 8 years ago

Why the KMPlaceholderTextView still has NSAutoresizingMaskLayoutConstraint?

k3zi commented 8 years ago

It seems I was setting up my constraints to late so the display loaded the view with translatesAutoresizingMaskIntoConstraints set to true.

It still seems that the view is being over constrained though. You set the leading and trailing distance in accordance with the inset but then you also set the width equal to the container. You would only have to to set either:

Setting all three would be unnecessary.

MoZhouqi commented 8 years ago

Adding the width constraint is to fix this issue https://github.com/MoZhouqi/KMPlaceholderTextView/issues/9.

k3zi commented 8 years ago

Oh, I see... The way to go then would be to disable one of the leading/trailing constraints, right?

MoZhouqi commented 8 years ago

Why don't you consider to set the translatesAutoresizingMaskIntoConstraints property to false?

MoZhouqi commented 8 years ago

Fixed in d670bb34adb98f6f4e328c5e29ed8eafd84838f6. Thanks for the awesome details!