Reputation: 470
I'm adding a UIButton in code, but when I test it on different devices the button doesn't resize and reposition itself correctly, however when I add the same button on a Storyboard it behaves properly... The storyboard does not have Autolayout enabled...
EDIT3: Added photo of UI below, the larger "Basics 2" button is the storyboard defined one....the rest are defined in code
EDIT2: When I look at the Storyboard XML for the button that behaves correctly it's defined like this, full storyboard XML now below
<button hidden="YES" autoresizesSubviews="NO" opaque="NO" contentMode="center" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" id="ujO-uz-KyO">
<rect key="frame" x="30" y="100" width="116" height="70"/>
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" widthSizable="YES" flexibleMaxX="YES" flexibleMinY="YES" heightSizable="YES" flexibleMaxY="YES"/>
<inset key="imageEdgeInsets" minX="0.0" minY="27" maxX="72" maxY="0.0"/>
<state key="normal" image="greenp.png" backgroundImage="basics2.png">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
</button>
I add the button like this (Edited to use the code suggested below, still doesn't work)
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = [[butLocArr objectAtIndex:x] CGRectValue];
button1.autoresizesSubviews = YES;
[button1 setAutoresizingMask: UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleBottomMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight];
//The view is a UiScrollView
[scrollChallengeView addSubview:button1];
The storyboard added button, which behaves correctly, is setup like this, where am i going wrong?
Upvotes: 1
Views: 704
Reputation: 10215
Set UIViewAutoresizingFlexibleWidth
and UIViewAutoresizingFlexibleHeight
[button1 setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
When you add button in IB with AutoresizingMask it's autoresize after lunch. For example, screen width increases from 320 (in storyboard) to 540px (on device) and button size increases proportionally. When you add button programmatically it's just added and you should calculate button size manually according to screen size. Autoresizing button will be calling on rotating. So you width and height should be calculated like this:
CGFloat width = k1 * [UIScreen mainScreen].bounds.size.width;
CGFloat height = k2 * width;
Upvotes: 1
Reputation: 2037
In that IB setup you've got:
So the autoresizing mask should be
button1.autoresizingMask =
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleBottomMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight;
Upvotes: 0
Reputation: 531
you need to use UIViewAutoresizingFlexibleMargins
i.e
[button1 setAutoresizingMask:UIViewAutoresizingFlexibleMargins];
or
[button1 setAutoresizingMask:UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin];
instead
[button1 setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
Upvotes: 0