Why does UIButton sometimes lost width constriain? - ios

There's a UIButton which does transition between two controllers. Either the two controllers have the same functionality of flipping to each other. It works pretty well, except that "sometimes" the button's width shrinks, while most of the time it looks just normal. The height and width of the button are both set to 35, a baseline constraint with its previous object and a trailing constraint with its superview in the storyboard is also declared. The background of the button is assigned to a image set in images.xcassets file. Is there any possible reason that the button width sometimes shrinks?
Normal state of the UIButton
And sometimes the UIButton thinks like this

It looks like the image is making space for title. This may be the case if you're using a standard UIButton. Try setting it to a custom type then you will have more control over the survives it contains.

Related

How to control layout of UIBarButtonItem controls on an iOS toolbar?

On my toolbar I am trying to properly lay out three buttons. One in the middle that should remain centered horizontally, and then one aligned left and one aligned right. But because the text for the left button is longer than either the middle button or the right one, the spacer items won't seem to produce the desired spacing effect. I've tried both fixed and flexible, but of course using the fixed type will not dynamically respond to different screen widths, and the the flexible width ones (UIBarButtonSystemItemFlexibleSpace) will not keep the middle button centered.
I've defined the layout both in Interface Builder and programmatically and I get the same effect, what you see here. For example an iPhone 6s looks like:
and an iPhone 11 Pro looks like:
As you can see the "Mid" button is not centered. By using UIBarButtonSystemItemFixedSpace instead of UIBarButtonSystemItemFlexibleSpace I could define the width of the spacers exactly, but in order to calculate it properly I would need to know the width of each UIBarButtonItem and also the right delegate method to do this in. Is this even possible, or must I do a custom view (or something else) to make this UIToolbar look like I want?
Try adding leading spaces to the Shorter title OR using the following items:
Longer-Text Button, Flexible Space, Mid Button, Flexible Space, Fixed Space, Shorter Button
The fixed space would be a hard coded value to match the width delta between the two buttons.

Wrapping text in UITextView in Swift

UPDATE: Looks like the UITextView's width is bigger than screen's width.
It is strange since I created the UITextView using a storyboard and in preview mode it looks like it's width is less than that of the UIViewController.
I have a UITextView, in a UIViewController, that was created using storyboard.
I have similar code to populate the text in the UITextView.
self.taskGroupDescriptionTextView.text = taskGroup.description
self.taskGroupDescriptionTextView.textContainer.lineBreakMode = NSLineBreakMode.ByCharWrapping
I tried different types of lineBreakMode, break by char/word. None of them work.
My UIViewController when run on iOS simulator still has text that extends beyond the screen.
You should set constraints. Here's an example how to do it:
You click on your UITextView so that you can start editing it. At the bottom of the storyboard there are few icons and clicking on the one shown in the image will open up an editor where you can add constraints. You have plenty of options (pin to left, right, top, bottom, fix height or width, aspect ratio,... and even more if you click on a neighbour icon). Keep in mind that you have to set enough constraint so that it will be reasonable for your view controller to calculate the frame size of your view. But no worries, if you'll forget something you'll get an error.

Why does UIButton resize when I access titleLabel property?

I'm trying to adjust the size of a button to (it's intrinsic size + a little bit more) in order to draw a custom background. However, every time I access self.titleLabel within the button, the size and position resnaps to that of the storyboard. I don't have to do anything with the label to reproduce this, just retrieve it from the button.
I've put logging code all over my button and view controller in order to find where this is happening. It's not coming from a relaying-out of subviews or any other notification I see to get within the view controller. The line before accessing titleLabel, the position and size are correct. The line after, it has snapped back to the storyboard position. Commenting out the access prevents the size/position snapping. Can someone tell me where or why this is happening?
I have no constraints set (that I can tell), but am I fighting against auto-layout here? Should I be doing this a different way like composing controls or something?
Something similar (or the same?) has been asked before at UIButton modifying titlelabel seems to change its frame and UIButton titleLabel resizes on press?, but both were left unanswered or explained away with just "maybe a bug."
If the project has auto-layout enabled, then YES, you're fighting auto-layout. You have two choices, either subclass UIButton so that you can override the intrinsic size calculation, or modify the constraints so that the intrinsic size is not used in any constraint. If you do the latter, then you probably want to create an IBOutlet to the constraint for the width, so that you can adjust the constant property as needed.
This isn't a bug, it's a consequence of auto layout. When using auto layout, you shouldn't set any frames. Instead, you should change the size or position by modifying the constraints. What's happening, is that whenever the view needs to be redrawn, the frame reverts to the frame that's defined by the constraints.

UIButton changing width when label changes

I'm writing a calculator app with a shift key. When the shift key is pressed, some of the UIButtons' labels should change. The problem is that when they do, the widths also change and, because of certain design constraints, other buttons will resize themselves too. How to I lock the width of a button (either pragmatically or in the storyboard builder) so that it will never change (regardless of the contents of the button)
Thanks for any help in advance.
Buttons, like labels have an intrinsic size that's set by the width of their text plus some padding. If you want them to have a fixed width, then in IB, just give each button an explicit width from the menu, Editor --> Pin --> Width.
Have you tried using [button sizeToFit]? Call it after you resize the subviews within and then you will likely want to apply some "padding" as this will size the view to exactly fit it's subviews.
UIView sizeToFit

iOS: UILabel & UIButton in a table cell

I am trying to display a Label and a button side-by-side in a UITableViewCell. The button sits at the right end & the Label sits at the left end. The label can host a long text and should get truncated if the text would flow out of bounds (I do not want to reduce the font size, etc.). Needless to say, I want this to work for both the orientations.
If I use UITableViewCellStyleDefault (without adding a button) & set a long text for the default label, I observe that the label auto-resizes perfectly when the orientation changes. Most probably because its autoresizingMask is set to UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin. Now, the way I see it, I could insert my button, and make the label truncate properly, if I could configure the value of the right margin that is being used by default (to accommodate the button). I essentially want it to auto-resize within the bounds that I specify. Is there a way this can be accomplished?
I would not prefer listening to each orientation change and setting the bounds of the label's frame. Any other feasible solution?
I finally went with subclassing UITableViewCell & overriding layoutSubViews. Thanks Mark!

Resources