使用自动布局为iOS8今日扩展设置固定高度

时间:2014-09-19 04:43:56

标签: ios ios8 autolayout today-extension

我正在尝试实现一个简单的今天小部件,其中包含一个固定高度的单个标签。

在文档中说:

  

如果窗口小部件包含要显示的其他内容,则可以依赖“自动”   布局约束以适当调整窗口小部件的高度。

但是我一直得到“无法同时满足约束”的警告。

以下是我的尝试:

  1. 创建一个新的“今日扩展”目标,它会创建一个带有“Hello World”标签和视图控制器的故事板。
  2. 将高度限制设置为“Hello World”标签。
  3. enter image description here

    当我跑步时,我得到:

    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.
    

    我做错了什么?使用自动布局创建具有固定高度的窗口小部件的正确方法是什么?

2 个答案:

答案 0 :(得分:1)

我找到了一种方法来通过引入包装器视图来删除这些警告,将所有其他视图放入此包装器视图中,并为包装器视图的高度约束设置低优先级。

snapshot

答案 1 :(得分:0)

你需要做的是设置标签的高度,宽度,前导和拖尾,并使标签中心X和Y.当给你的应用程序运行你的应用程序时它会保持距离,这会发生什么?是这样你会看到拉伸。如果你想使用top,top,trailing,bottom中的任何一个,你需要给出底部空间&#39;&gt; =&#39;它增加了superview的底部间距。简单易行的解决方案是从superview制作centerX和centerY。它会将视图准确放置在屏幕或视图的中心。

学习目的访问:
简单:
http://technet.weblineindia.com/mobile/using-auto-layout-in-xcode-6-for-universal-ios-app-development/进展:
http://www.youtube.com/watch?v=G53PuA_TlXk&feature=youtu.be&list=UUtc1Jt_UTPsXpAGtvlr0nUQ

如您所见,我只设置了centerX,centerY,width,height,leading和trailing space