2

I am implementing a today widget. This is my first time getting hands onto the today widget.

I created my today widget programmatically without using storyboard. Learned from this POST, What I did: 1. change the info plist 2. enable "Embedded Content Contains Swift Code" 3. add @objc(HGTodayViewController) after import where HGTodayViewController is my inital View Controller

in loadView in HGTodayViewController

var mainView:HGTodayView!

override func loadView() {
  self.mainView = HGTodayView(frame: CGRectZero)
  self.view = self.mainView
}

in 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()
}

In HGTodayView setConstraint Method:

  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)
  }

Because I haven't setup table view delegate for it, I suspect it does not generate any cells at the moment and It displays a red table view as it supposed to do. However, if I go ahead press the lock button to lock the device, it immediately pops out a conflict in constraint msg in the console.

Here is the conflict in constraint message:

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.

This conflict in constraint seems occur when I lock my phone. How can I solve this conflict? Any help is appreciated. Thank you very much for your time.

Community
  • 1
  • 1
nuynait
  • 1,912
  • 20
  • 27
  • Did you manage to get rid of that NSLayoutConstraint conflict when locking your device? – Rygen Aug 31 '16 at 22:23

0 Answers0