Harrison Bateman
Harrison Bateman

Reputation: 49

Constraints Overlapping, Swift Xcode

I'm creating a custom keyboard in Xcode with swift. Everything runs great but I am running into a problem with constraints. Ill explain what I've done and what I am looking to do.

what I have done:1)I have created a 'world' button that will switch between the iOS default keyboard and the custom keyboard. It is constrained to the bottom left of the view, no matter what device it is loaded onto (iPhone 5,6,7 iPad etc). 2)I have then created a collection view that is constrained to start at the edge of the world button no matter the device. 3)I have created a delete button that is constrained to the bottom right of the view, no matter the device.

what I want to do: 1)I want the collection view to start at the world button and end at the delete button, no matter the device.

The trouble I am having is that the delete button overlaps the collection view on smaller devices. I want the collection view to stop at the delete button but cannot figure out why my constraints are not working.

Custom Keyboard screenshot

These are the relevant constraints for the collection view.

// create the constraints

// leading constraint
let categoriesCollectionViewLeadingConstraint = NSLayoutConstraint(item: categoriesCollectionView, attribute: NSLayoutAttribute.leading, relatedBy:    NSLayoutRelation.equal, toItem: backButton, attribute:   NSLayoutAttribute.trailing, multiplier: 1, constant: 0)

// add the leading constraint
view.addConstraint(categoriesCollectionViewLeadingConstraint)

// bottom constraint
let categoriesCollectionViewBottomConstraint = NSLayoutConstraint(item: categoriesCollectionView, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 0)

// add the bottom constraint
view.addConstraint(categoriesCollectionViewBottomConstraint)

// trailing constraint
let categoriesCollectionViewTrailingConstraint = NSLayoutConstraint(item: categoriesCollectionView, attribute: NSLayoutAttribute.trailing, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.trailing, multiplier: 1, constant: 0)

// set the priority to less than 1000 so it works correctly
categoriesCollectionViewTrailingConstraint.priority = 999

// add the trailing constraint
view.addConstraint(categoriesCollectionViewTrailingConstraint)

Upvotes: 1

Views: 675

Answers (1)

3stud1ant3
3stud1ant3

Reputation: 3606

I think you should constraint your collection view like this:

   // trailing constraint
let categoriesCollectionViewTrailingConstraint = NSLayoutConstraint(item: categoriesCollectionView, attribute: NSLayoutAttribute.trailing, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.trailing, multiplier: 1, constant: - deleteButtonWidth)

Upvotes: 1

Related Questions