1

In iPhone8 like rectangle edge devices, I need a bottom button stuck on the bottom edge with sharp corners. And in iPhone12 like curved edge devices, I need a bottom button with some distance on right-left-bottom with a corner radius.

How can I manage it on the storyboard? Please check the below image for better understanding.

enter image description here

enter image description here

Krutika Sonawala
  • 1,065
  • 1
  • 12
  • 30
  • How you set constraints for that button in a storyboard, can you please add your code or a storyboard image? because you have to set leading, trailing, and bottom space to `safeArea` to `superview` to achieve result – Kishan Bhatiya Apr 22 '21 at 06:29
  • @KishanBhatiya Added. Please check. – Krutika Sonawala Apr 22 '21 at 06:41
  • check this: https://stackoverflow.com/questions/46880364/iphone-x-aligning-object-bottom-to-safe-area-ruins-look-on-other-devices – Kishan Bhatiya Apr 22 '21 at 07:07
  • You need to give leading and trailing of Button to View then it will have left and right spaces. Remove the Width constraint of Button. – Kudos Apr 22 '21 at 07:42

2 Answers2

1

As far as I know, the storyboard has no straightforward way to achieve this. If I were you, I'll

  1. Check whether the device has curved edges(i.e the device is iPhone X or higher model) or not and then
  2. Set button constraints programmatically based on the above check.

There's no official API provided for checking whether the device has curved edges or not but here's my solution so if anyone knows a better hack, please let me know.

extension UIDevice {
    var iPhoneXOrAboveModel: Bool {
        return UIScreen.main.nativeBounds.height >= 2436
    }
}

Now, you can use this extension inside your view controller and constraint your button.

if UIDevice.current.iPhoneXOrAboveModel {
            // Has curved edges
            NSLayoutConstraint.activate([
                button.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
                button.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant:  -20)
            ])
            button.layer.cornerRadius = 12
        } else {
            // Has square edges
            NSLayoutConstraint.activate([
                button.leadingAnchor.constraint(equalTo: view.leadingAnchor),
                button.trailingAnchor.constraint(equalTo: view.trailingAnchor)
            ])
        }
KyawTheMonkey
  • 181
  • 1
  • 6
1
  1. Remove Width Constraint, Equal with to Superview constraint with you have given.
  2. Provide leading and trailing constraint to Button.
  3. Check image below.

Look at my constraints

Kudos
  • 1,224
  • 9
  • 21
  • and what about square edge devices? – Krutika Sonawala Apr 22 '21 at 11:51
  • 1
    @krutika you need to take care of only bottom space. In edge devices button will have 0 space from bottom bcz they dont have SafeAreaView. And Notch devices has SafeareaView, And by default we give Bottom spaces from SafeAreaView in Notch Devices. – Kudos Apr 22 '21 at 12:11
  • 1
    @krutika to fix Bottom space issue in Notch devices you need to give Bottom space from SuperView instead of SafeArea. – Kudos Apr 22 '21 at 12:13