From 1f5b35564b82ae7d23dcd14b1aa985bd21ce4c17 Mon Sep 17 00:00:00 2001 From: Jordan Hipwell Date: Sat, 16 Sep 2017 10:03:21 -0600 Subject: [PATCH] Update center button position for iPhone X --- .../HELargeCenterTabBarController.swift | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/HELargeCenterTabBarController/HELargeCenterTabBarController.swift b/HELargeCenterTabBarController/HELargeCenterTabBarController.swift index 384b1c4..a99a7e8 100644 --- a/HELargeCenterTabBarController/HELargeCenterTabBarController.swift +++ b/HELargeCenterTabBarController/HELargeCenterTabBarController.swift @@ -145,10 +145,19 @@ class HELargeCenterTabBarController: UITabBarController { button.translatesAutoresizingMaskIntoConstraints = false tabBar.addSubview(button) - view.addConstraint(NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.centerX, multiplier: 1.0, constant: 0.0)); - view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[button]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["button": button])); - view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[button(==\(unselectedImage.size.width))]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["button": button])); - view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[button(==\(unselectedImage.size.height))]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["button": button])); + if #available(iOS 11.0, *) { + NSLayoutConstraint.activate([ + button.centerXAnchor.constraint(equalTo: view.centerXAnchor), + button.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor), + button.widthAnchor.constraint(equalToConstant: unselectedImage.size.width), + button.heightAnchor.constraint(equalToConstant: unselectedImage.size.height) + ]) + } else { + view.addConstraint(NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.centerX, multiplier: 1.0, constant: 0.0)) + view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[button]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["button": button])); + view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[button(==\(unselectedImage.size.width))]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["button": button])) + view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[button(==\(unselectedImage.size.height))]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["button": button])) + } button.addTarget(self, action: #selector(centerButtonAction(_:)), for: .touchUpInside) centerButton = button