Helosy
Helosy

Reputation: 359

ios - Adjust button width and height based on screen size

I am trying to adjust button sizes according to the device they are run on. iPhone SE is small compared to iPhone 8 and as a result the buttons do not fully appear.

iphone SE simulator buttons not fully showing

I tried using the following code to adjust the size of the buttons according to the screen size but it did not show any changes.

roundedCornerDeliveryButton.layer.cornerRadius = 8
roundedCornerKitHomeButton.layer.cornerRadius = 8
widthMultiplier = Double(self.view.frame.size.width) / 69
heightMultiplier = Double(self.view.frame.size.height) / 321
roundedCornerDeliveryButton.frame.size.width = roundedCornerDeliveryButton.frame.width * CGFloat(widthMultiplier)
roundedCornerDeliveryButton.frame.size.height = roundedCornerDeliveryButton.frame.height * CGFloat(heightMultiplier)
roundedCornerKitHomeButton.frame.size.width = roundedCornerKitHomeButton.frame.width * CGFloat(widthMultiplier)
roundedCornerKitHomeButton.frame.size.height = roundedCornerKitHomeButton.frame.height * CGFloat(heightMultiplier)
roundedCornerDeliveryButton.frame.origin = CGPoint(x: roundedCornerDeliveryButton.frame.origin.x * CGFloat(widthMultiplier), y: roundedCornerDeliveryButton.frame.origin.y * CGFloat(heightMultiplier))
roundedCornerKitHomeButton.frame.origin = CGPoint(x: roundedCornerKitHomeButton.frame.origin.x * CGFloat(widthMultiplier), y: roundedCornerKitHomeButton.frame.origin.y * CGFloat(heightMultiplier))

How would I do this?

Upvotes: 1

Views: 6174

Answers (2)

ekscrypto
ekscrypto

Reputation: 3806

There are a few ways to do this, but it comes down to how you declared your buttons in the first place.

If your buttons are declared in Storyboard or Xib file, you probably should be using layout constraints.

For example, if you want a button to take 1/3rd, you start by defining a layout constraint with the top view of the view controller with "Equal Width", then you edit that constraint and change its multiplier to 1:3

enter image description here

The layout system will do its magic to ensure the constraints is respected and the button is always 1/3rd the screen width.

You can declare several constraints like that to automatically respect different constraints, like making sure your button height is always taller than 36pt, width is never wider than 400pt, etc. Just have to define the proper priorities and the constraints.

Defining your sizing constraints this way has the advantage of being inspectable in the Xib as you can quickly change device type & orientation and make sure everything works before even running your code.

Good luck!

Upvotes: 4

Shehata Gamal
Shehata Gamal

Reputation: 100503

To make the button fit its content use

button.sizeToFit()

Also it's better to do it with auto-layout

self.view.addSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
   button.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
   button.centerYAnchor.constraint(equalTo: self.view.centerYAnchor)
])

You can add this constraint if you want it proportionally

button.widthAnchor.constraint(equalTo:self.view.widthAnchor,multiplier:0.75)

Upvotes: 1

Related Questions