1

Two buttons in UIView

I essentially have two buttons laid out in a larger view, and I want the distance between the buttons to be equal to a multiple of the larger view. For example, I want to do something like:

var buttonOneRightConstraint = NSLayoutConstraint(item: button1, attribute: .Right, relatedBy: .Equal, toItem: button2, attribute: .Left, multiplier: 1.0, constant: -largerView.frame.width/10)

Which works well in one orientation, but doesn't update when the view size changes (e.g., device is rotated).

I think I would want to have it calculate largerView.frame.width every time the view is updated, as opposed to treat it like a constant, but not sure how to do that!

vk2015
  • 1,118
  • 1
  • 12
  • 16

1 Answers1

0

Add an observer for the device orientation change event in didFinishLaunchingWithOptions method:

  NSNotificationCenter.defaultCenter().addObserver(TheClassYouWantTheMethodToBeFired, selector: "rotated", name: UIDeviceOrientationDidChangeNotification, object: nil)

Then, compute the layout inside the rotated new method

How to detect orientation change?

Community
  • 1
  • 1
andriosr
  • 481
  • 4
  • 12
  • Sorry, I'm a bit new to Swift - where would you put this if you are using an extension? I'm creating a custom third-party keyboard. – vk2015 Jul 27 '16 at 15:30