Uibuttons Are Not Correctly Shaped on All Devices

UIButtons are not correctly shaped on all devices

set button cornerRadius in viewDidLayoutSubviews method of viewcontroller

 override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
// Edit it to round
topLeftButtonImage.layer.cornerRadius = topLeftButtonImage.bounds.size.height / 2
topLeftButtonImage.clipsToBounds = true

// Add border
topLeftButtonImage.layer.borderColor = UIColor.white.cgColor // Button border color
topLeftButtonImage.layer.borderWidth = 4 // Button border width
}

UIButtons not working on certain devices

Well,

I clicked on "Resolve Auto Layout Issues" - "Reset to suggested constraints" (third from the left):

Sample Image

and that "solved" my problem. Everything is working on all devices. It seems to have removed some constraints that I put in place.

I'm going to have to learn more about why this worked.

Arranging UIButtons evenly on 3.5” and 4” display

The only way I've gotten this to work is by putting labels (with no titles, so they're invisible) between the edges and between the buttons, so that the buttons and labels take up all the vertical space on the screen. The buttons have an intrinsic size, but the labels don't, so they expand or contract to fill the space correctly.

-(void)viewDidLoad {
[super viewDidLoad];

NSMutableDictionary *viewsDict = [NSMutableDictionary dictionary];
NSArray *titles = @[@"Button 1",@"Button 2",@"Button 3",@"Button 4",@"Button 5"];
for (int i=1; i<6; i++) {
UIButton *b = [UIButton buttonWithType:1];
[b setTitle:titles[i-1] forState:UIControlStateNormal];
[b setTranslatesAutoresizingMaskIntoConstraints:NO];
[viewsDict setObject:b forKey:[NSString stringWithFormat:@"b%d",i]];
}

for (int i=1; i<7; i++) { // labels for spacing
UILabel *l = [[UILabel alloc ]init];
[l setTranslatesAutoresizingMaskIntoConstraints:NO];
[viewsDict setObject:l forKey:[NSString stringWithFormat:@"l%d",i]];
}

for (id obj in viewsDict.allKeys)
[self.view addSubview:viewsDict[obj]];

NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[l1][b1][l2(==l1)][b2][l3(==l1)][b3][l4(==l1)][b4][l5(==l1)][b5][l6(==l1)]|"
options:NSLayoutFormatAlignAllLeading
metrics:nil
views:viewsDict];

NSArray *constraints2 = [NSLayoutConstraint constraintsWithVisualFormat:@"|-100-[b1]"
options:NSLayoutFormatAlignAllLeading
metrics:nil
views:viewsDict];

[self.view addConstraints:constraints];
[self.view addConstraints:constraints2];
}

This aligns all the buttons 100 points from the left edge, with equal spacing between the buttons. This will adjust for screen size as well as rotation.

iOS 11 UIBarButtonItem images not sizing

BarButtonItem (iOS11\xCode9) uses autolayout instead of frames. Try this (Swift):

if #available(iOS 9.0, *) {
cButton.widthAnchor.constraint(equalToConstant: customViewButton.width).isActive = true
cButton.heightAnchor.constraint(equalToConstant: customViewButton.height).isActive = true
}

Objective C

if (@available(iOS 9, *)) {
[cButton.widthAnchor constraintEqualToConstant: standardButtonSize.width].active = YES;
[cButton.heightAnchor constraintEqualToConstant: standardButtonSize.height].active = YES;
}

iOS: Is it possible for UI objects to respect safe area without auto-layout in storyboard?

Turns out the answer is easier than I thought. Just put all the UIButton's inside another view. Then add constraints to that view that respects the safe areas. This way the UIbuttons can still use autoresizing.

Migrating a project using autoresizing masks for iPhone X

UIButton image behavior changed in iOS 15?

Is this a change in iOS 15?

Yes and no. There is indeed a change in iOS 15, but the reason for the problem you're experiencing is a change in Xcode 13.

The change in iOS 15 is that there's a whole new way of configuring a button. This starts with giving the button one of four new iOS 15 types: Plain, Gray, Tinted, and Filled. If you set your button to have any of those types, you are opting in to the new behavior.

The problem you're seeing is because, in Xcode 13, when you make a button in the storyboard, it does give the button one of those types: Plain. So you have opted into the new dispensation without knowing it!

The solution, if you want the old behavior, is to change the Style pop-up menu (in the Attributes inspector) from Plain to Default. Now you have an old-style button and it will behave in the way you're accustomed to.

(Of course, in the long run, you're going to want to adopt the new dispensation. I'm just explaining the apparent change in behavior.)

Why is my UIImageView's frame greater than the screen size?

Layout has not yet happened in viewDidLoad. The size you get there is meaningless. Ignore it. If you want to know how big the image view will really be, wait until later in the birth cycle of you view controller, such as viewDidLayoutSubviews.

Problem with Aligning of buttons in Stack View

How you set your stack view properties and constraints, and the button constraints, will depend on exactly what you want as a result.

Take a look at these image - first one includes the Editor -> Canvas -> Bounds Rectangles:

Sample Image

Sample Image

In all three, Button 1 has a 1:1 Ratio constraint.


The Top stack view is constrained Top (40) and Leading and Trailing at 16-pts from the sides. Stack view properties are:

  • Alignment: Fill
  • Distribution: Fill Equally
  • Spacing: Standard

So, the stack view width is the width of the view, minus 32 (16-pts on each side). The stack view automatically gives each button the same width - Fill Equally - and the 1:1 constraint on the first button sets its Height equal to its Width (square), and the rest of the buttons follow suit.


For the Middle example, the stack view is constrained Top (40) and CenterX. Stack view properties are:

  • Alignment: Fill
  • Distribution: Fill Equally
  • Spacing: Standard

The only additional constraint is Button 1 has a Width constraint of 40. So all 6 buttons are 40 x 40, standard spacing between them, and the stack view is centered horizontally.


For the Bottom example, the stack view is constrained Top (40) and Leading and Trailing at 16-pts from the sides. Stack view properties are:

  • Alignment: Fill
  • Distribution: Equal Spacing
  • Spacing: Standard

In addition, Button 1 has a width constraint of 40, and Buttons 2 thru 6 all have Width = Button1.width.

So the stack view width is the width of the view, minus 32 (16-pts on each side). Each button is 40 x 40 (again, the 1:1 ratio on Button 1 keeps them square), and the stack view set the spacing to fill out the width.


You'll want to keep in mind how that will change on different device sizes.

For example, here's how that looks (with NO changes to constraints or properties) when we rotate the device:

Sample Image


Here is how the structure looks in Storyboard:

Sample Image

and here's the source to the Storyboard so you can examine / play with it:

<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="16096" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<device id="retina4_7" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="16087"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="Stack View standard spacing" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="o2J-7j-Hec">
<objects>
<viewController id="qIB-4e-obb" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="Aak-ib-U1S">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" distribution="fillEqually" spacingType="standard" translatesAutoresizingMaskIntoConstraints="NO" id="DHB-rx-egj" userLabel="FitWidthStack">
<rect key="frame" x="16" y="40" width="343" height="50.5"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="8Ue-Xm-LQi">
<rect key="frame" x="0.0" y="0.0" width="50.5" height="50.5"/>
<color key="backgroundColor" red="0.55634254220000001" green="0.97934550050000002" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstAttribute="width" secondItem="8Ue-Xm-LQi" secondAttribute="height" multiplier="1:1" id="IvC-Jh-rdd"/>
</constraints>
<state key="normal" title="1"/>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="R14-hZ-UbE">
<rect key="frame" x="58.5" y="0.0" width="50.5" height="50.5"/>
<color key="backgroundColor" red="0.55634254220000001" green="0.97934550050000002" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<state key="normal" title="2"/>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="syB-R1-vN7">
<rect key="frame" x="117" y="0.0" width="50.5" height="50.5"/>
<color key="backgroundColor" red="0.55634254220000001" green="0.97934550050000002" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<state key="normal" title="3"/>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="f1X-gj-ygM">
<rect key="frame" x="175.5" y="0.0" width="50.5" height="50.5"/>
<color key="backgroundColor" red="0.55634254220000001" green="0.97934550050000002" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<state key="normal" title="4"/>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="fK2-cI-wo3">
<rect key="frame" x="234" y="0.0" width="50.5" height="50.5"/>
<color key="backgroundColor" red="0.55634254220000001" green="0.97934550050000002" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<state key="normal" title="5"/>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="MQX-O8-Tgu">
<rect key="frame" x="292.5" y="0.0" width="50.5" height="50.5"/>
<color key="backgroundColor" red="0.55634254220000001" green="0.97934550050000002" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<state key="normal" title="6"/>
</button>
</subviews>
</stackView>
<stackView opaque="NO" contentMode="scaleToFill" distribution="fillEqually" spacingType="standard" translatesAutoresizingMaskIntoConstraints="NO" id="D9y-7o-SD4" userLabel="CenterXStack">
<rect key="frame" x="47.5" y="130.5" width="280" height="40"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="PVs-vi-HFL">
<rect key="frame" x="0.0" y="0.0" width="40" height="40"/>
<color key="backgroundColor" red="0.55634254220000001" green="0.97934550050000002" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstAttribute="width" constant="40" id="2tP-1p-WIh"/>
<constraint firstAttribute="width" secondItem="PVs-vi-HFL" secondAttribute="height" multiplier="1:1" id="iph-ol-vl6"/>
</constraints>
<state key="normal" title="1"/>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Zam-9o-U4q">
<rect key="frame" x="48" y="0.0" width="40" height="40"/>
<color key="backgroundColor" red="0.55634254220000001" green="0.97934550050000002" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<state key="normal" title="2"/>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Fdn-MX-Ivu">
<rect key="frame" x="96" y="0.0" width="40" height="40"/>
<color key="backgroundColor" red="0.55634254220000001" green="0.97934550050000002" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<state key="normal" title="3"/>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="dhX-aw-ohs">
<rect key="frame" x="144" y="0.0" width="40" height="40"/>
<color key="backgroundColor" red="0.55634254220000001" green="0.97934550050000002" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<state key="normal" title="4"/>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="nJO-xF-B3Y">
<rect key="frame" x="192" y="0.0" width="40" height="40"/>
<color key="backgroundColor" red="0.55634254220000001" green="0.97934550050000002" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<state key="normal" title="5"/>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="JZ6-Lc-yzW">
<rect key="frame" x="240" y="0.0" width="40" height="40"/>
<color key="backgroundColor" red="0.55634254220000001" green="0.97934550050000002" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<state key="normal" title="6"/>
</button>
</subviews>
</stackView>
<stackView opaque="NO" contentMode="scaleToFill" distribution="equalSpacing" spacingType="standard" translatesAutoresizingMaskIntoConstraints="NO" id="2eO-xS-xt7" userLabel="EqualSpacingStack">
<rect key="frame" x="16" y="210.5" width="343" height="40"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="5OX-TX-zzE">
<rect key="frame" x="0.0" y="0.0" width="40" height="40"/>
<color key="backgroundColor" red="0.55634254220000001" green="0.97934550050000002" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstAttribute="width" constant="40" id="FaE-ZG-cnh"/>
<constraint firstAttribute="width" secondItem="5OX-TX-zzE" secondAttribute="height" multiplier="1:1" id="yEH-j6-Ikc"/>
</constraints>
<state key="normal" title="1"/>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="vy5-wT-KZg">
<rect key="frame" x="60.5" y="0.0" width="40" height="40"/>
<color key="backgroundColor" red="0.55634254220000001" green="0.97934550050000002" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<state key="normal" title="2"/>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="xIu-el-0yH">
<rect key="frame" x="121" y="0.0" width="40" height="40"/>
<color key="backgroundColor" red="0.55634254220000001" green="0.97934550050000002" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<state key="normal" title="3"/>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Suq-hc-eDl">
<rect key="frame" x="182" y="0.0" width="40" height="40"/>
<color key="backgroundColor" red="0.55634254220000001" green="0.97934550050000002" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<state key="normal" title="4"/>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="hP2-U2-MNf">
<rect key="frame" x="242.5" y="0.0" width="40" height="40"/>
<color key="backgroundColor" red="0.55634254220000001" green="0.97934550050000002" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<state key="normal" title="5"/>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="kb1-3H-bP7">
<rect key="frame" x="303" y="0.0" width="40" height="40"/>
<color key="backgroundColor" red="0.55634254220000001" green="0.97934550050000002" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<state key="normal" title="6"/>
</button>
</subviews>
<constraints>
<constraint firstItem="kb1-3H-bP7" firstAttribute="width" secondItem="5OX-TX-zzE" secondAttribute="width" id="AMt-Sm-61b"/>
<constraint firstItem="Suq-hc-eDl" firstAttribute="width" secondItem="5OX-TX-zzE" secondAttribute="width" id="Exj-gK-x8q"/>
<constraint firstItem="hP2-U2-MNf" firstAttribute="width" secondItem="5OX-TX-zzE" secondAttribute="width" id="mKn-cn-wIQ"/>
<constraint firstItem="vy5-wT-KZg" firstAttribute="width" secondItem="5OX-TX-zzE" secondAttribute="width" id="rNn-Ua-n1n"/>
<constraint firstItem="xIu-el-0yH" firstAttribute="width" secondItem="5OX-TX-zzE" secondAttribute="width" id="vJn-JR-owY"/>
</constraints>
</stackView>
</subviews>
<color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
<constraints>


Related Topics



Leave a reply



Submit