kbtombul
kbtombul

Reputation: 489

Setting fixed height for iOS8 today extension using auto layout

I am trying to implement a simple today widget which contains a single label with fixed height.

In the documentation it says:

If a widget has additional content to display, you can rely on Auto Layout constraints to adjust the widget’s height as appropriate.

However I keep getting "Unable to simultaneously satisfy constraints" warnings.

Here is what I tried:

  1. Create a new "Today Extension" target, it creates the storyboard with a "Hello World" label and view controller.
  2. Set height constraint to "Hello World" label.

enter image description here

And when I run I get:

2014-09-18 21:13:07.123 TestWidget[23381:871330] 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) 
(
    "<NSLayoutConstraint:0x7f87c31126d0 V:[UILabel:0x7f87c3113230'Hello World Fixed Height'(21)]>",
    "<NSLayoutConstraint:0x7f87c30235b0 V:[_UILayoutGuide:0x7f87c30222b0]-(NSSpace(8))-[UILabel:0x7f87c3113230'Hello World Fixed Height']>",
    "<NSLayoutConstraint:0x7f87c3023600 V:[UILabel:0x7f87c3113230'Hello World Fixed Height']-(NSSpace(8))-[_UILayoutGuide:0x7f87c3022f00]>",
    "<_UILayoutSupportConstraint:0x7f87c3021980 V:[_UILayoutGuide:0x7f87c30222b0(0)]>",
    "<_UILayoutSupportConstraint:0x7f87c30218a0 V:|-(0)-[_UILayoutGuide:0x7f87c30222b0]   (Names: '|':UIView:0x7f87c3112f20 )>",
    "<_UILayoutSupportConstraint:0x7f87c3023360 V:[_UILayoutGuide:0x7f87c3022f00(0)]>",
    "<_UILayoutSupportConstraint:0x7f87c3023300 _UILayoutGuide:0x7f87c3022f00.bottom == UIView:0x7f87c3112f20.bottom>",
    "<NSLayoutConstraint:0x7f87c3326710 'UIView-Encapsulated-Layout-Height' V:[UIView:0x7f87c3112f20(628)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7f87c31126d0 V:[UILabel:0x7f87c3113230'Hello World Fixed Height'(21)]>

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.

What am I doing wrong? What is the correct way to create a widget with fixed height using auto layout?

Upvotes: 7

Views: 1675

Answers (2)

Quanlong
Quanlong

Reputation: 25476

I found a way to remove those warnings by introducing a wrapper view, put all other views into this wrapper view and set a low priority to wrapper view's height constraint.

snapshot

Upvotes: 1

Dhaivat Vyas
Dhaivat Vyas

Reputation: 2928

Hi what you need to do is set labels height,width,leading and tailing and make label center X and Y. What happens when give leading,top,trailing,bottom is when you run your application it will keep distance as it is so you view will get stretched. If you want to use any of leading,top,trailing,bottom you need to give bottom space '>=' it increase bottom spacing from superview. The simple and easy solution is to make is centerX and centerY from superview. It will place view exactly in center of screen or view.

Fou Study purpose visit:
simple:
http://technet.weblineindia.com/mobile/using-auto-layout-in-xcode-6-for-universal-ios-app-development/
Advance:
http://www.youtube.com/watch?v=G53PuA_TlXk&feature=youtu.be&list=UUtc1Jt_UTPsXpAGtvlr0nUQ

As you can see I've just set centerX,centerY,width,height,leading and trailing space.

Upvotes: 0

Related Questions