我正在实现今天的小部件。这是我第一次接触今天的小部件。
我在不使用故事板的情况下以编程方式创建了今天的小部件。从这个POST中学到了,我做了什么: 1.更改信息plist 2.启用"嵌入式内容包含Swift Code" 3.导入后添加@objc(HGTodayViewController),其中HGTodayViewController是我的初始视图控制器
在HGTodayViewController中的loadView中
var mainView:HGTodayView!
override func loadView() {
self.mainView = HGTodayView(frame: CGRectZero)
self.view = self.mainView
}
在HGTodayView中的:
override init(frame: CGRect) {
super.init(frame: frame)
// self
self.translatesAutoresizingMaskIntoConstraints = false
// Subview
self.tableView = UITableView()
self.tableView.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(self.tableView)
// Constraint
self.setConstraint()
// debug
self.tableView.backgroundColor = UIColor.redColor()
}
在HGTodayView setConstraint方法中:
func setConstraint() {
// self.height
let selfHeight = NSLayoutConstraint(
item: self,
attribute: .Height,
relatedBy: .Equal,
toItem: nil,
attribute: .NotAnAttribute,
multiplier: 1, constant: 200)
// avoid constraint conflict with "UIView-Encapsulated-Layout-Height"
// https://stackoverflow.com/a/25795758/2581637
selfHeight.priority = 999
self.addConstraint(selfHeight)
let viewDict = Dictionary(dictionaryLiteral: ("tableView", self.tableView))
let tableViewHorizontallyLayout = NSLayoutConstraint.constraintsWithVisualFormat(
"|[tableView]|",
options: NSLayoutFormatOptions.DirectionLeadingToTrailing,
metrics: nil, views: viewDict)
self.addConstraints(tableViewHorizontallyLayout)
let tableViewVerticallyLayout = NSLayoutConstraint.constraintsWithVisualFormat(
"V:|[tableView]|",
options: NSLayoutFormatOptions.DirectionLeadingToTrailing,
metrics: nil, views: viewDict)
self.addConstraints(tableViewVerticallyLayout)
}
因为我还没有为它设置表视图委托,我怀疑它现在不会生成任何单元格,它会显示一个红色表格视图。但是,如果我继续按下锁定按钮来锁定设备,它会立即在控制台中弹出约束消息中的冲突。
以下是约束消息中的冲突:
2016-06-01 17:47:48.311 HGToday[4549:462502] 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.
(
"<NSLayoutConstraint:0x14fd50eb0 V:|-(4)-[UIInputSetContainerView:0x14fd5f570] (Names: '|':UITextEffectsWindowHosted:0x14fe6f670 )>",
"<NSLayoutConstraint:0x14fd6c7c0 'UIInputWindowController-top' V:|-(0)-[UIInputSetContainerView:0x14fd5f570] (Names: '|':UITextEffectsWindowHosted:0x14fe6f670 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x14fd50eb0 V:|-(4)-[UIInputSetContainerView:0x14fd5f570] (Names: '|':UITextEffectsWindowHosted:0x14fe6f670 )>
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.
2016-06-01 17:47:48.335 HGToday[4549:462502] 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.
(
"<NSLayoutConstraint:0x14fd63780 V:|-(4)-[UIInputSetContainerView:0x14fe70820] (Names: '|':UIRemoteKeyboardWindowHosted:0x14fe70490 )>",
"<NSLayoutConstraint:0x14fd65530 'UIInputWindowController-top' V:|-(0)-[UIInputSetContainerView:0x14fe70820] (Names: '|':UIRemoteKeyboardWindowHosted:0x14fe70490 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x14fd63780 V:|-(4)-[UIInputSetContainerView:0x14fe70820] (Names: '|':UIRemoteKeyboardWindowHosted:0x14fe70490 )>
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.
当我锁定手机时,似乎会发生这种约束冲突。我该如何解决这个冲突?任何帮助表示赞赏。非常感谢您的宝贵时间。