Creating UILabels with delete button - ios

For my iOS app, I want to create UILabels with a button attached to the top right. If the user presses the button, the UILabel will be deleted. Here is a visual of what I want to create :
I know how to build the UILabel with the rounded corners and text but I'm a little lost on how to attach the buttons on the top right. Is the best way to create this to attach individual buttons to the labels? If I do follow this approach, wouldn't this get messy with constraints?

Rounded Labels
let label = UILabel() // But can be an outlet.
label.layer.cornerRadius = label.frame.height / 2.0
(Sorry, just saw that you knew this bit. I just got excited.)
Buttons
The constraints shouldn't be messy. However you want to align the buttons with labels you're going to need to specify the horizontal and vertical constraints, and possibly height and width constraints.
Just in case you're unfamiliar with IB:
Add a vertical constraint between the label and button. Any will do for now.
Add a horizontal constraint between the label and button. Any will do for now.
Double-click the vertical constraint to refine it; set the button's Centre Y to the label's Top anchor.
Double-click the horizontal constraint to refine it; set the Centre X to the label's Trailing anchor.
I don't think the centre of the button is exactly on the corner, so use the Constant property to adjust.

Related

How to make the text on UILabel appear on the top-left corner after adding constraints?

I'm developing an iOS application that has a Segue where some information is displayed. The information that I'm displaying is appearing in the middle of the Segue and I want it to appear right below the X or close button.
I added the constraints necessary in the Storyboard and in the Storyboard I see it as I want it, but it's not translating when the application is ran. The text where I'm having problems can be seen in the picture below in the right, which is the Segue.
The following code snippet is meant to make the text start from the top left corner of the label text.
override func viewDidLoad() {
super.viewDidLoad()
infoLabel.text = text
infoLabel.numberOfLines = 0;
infoLabel.sizeToFit()
}
but after adding some constraints on that label to make it adapt to other screen sizes the text stopped starting on the top-left corner and started right in the middle-left corner. How can I fix this?
If you want to make label to constrain to right, don't give the leading constraint. Just the trailing, top constraints are enough for a label. Only give width constraint if you want label to take a fixed space.
In your case, just delete the leading constraint.
You can also play with the text alignment, set the text alignment to right if you want to keep your constraints intact.
You need to align the text and set to right alignment

UILabel right next to UIImage using autolayout

How would I go about creating a layout like this using autolayout?
The icon is UIImage, text is UILabel.
The label text can vary in width, but will always be single-line. How do I force the label to scale with the text horizontally and UIImage to be immediately to the left of the label?
I have tried using attributedText. I also tried views as containers and a horizontal stack view, but failed to get the result I wanted. What are the other options?
That should be absolutely no problem - just put in the image, make layout constraints to the left and top of the view, then add the label right to it, and make layout constraints between the right side of the image and the left side of the text.
To restrict the label width you can add a constraint from the right side of the label to the right side of the view, if you want to make the label as small as possible with a greater-than sign.
EDIT
If the whole should be bound to the right border of the superview, put both in a view, align the view to the right border of the superview, and make a constraint from the right side of the label to the right side of the view. The view will resize accordingly when the label resizes.
I went for a solution where I added a third view as the leftmost element in a horizontal stackview and set the stackview's distribution as 'equal centering'. It actually fit well with the rest of the planned layout (all elements are used efficiently). Thank you for your help guys.

iOS Autolayout: two buttons of equal width, side by side

I am currently having difficulty with AutoLayout. I am using interface builder and am trying to position two buttons of equal width side by side as illustrated in the following image.
From the following Preview image, my titleImage has been properly constrained and is displaying correctly however the buttons are not. I have experimented by aligning button 1 with the leading edge of titleImage, and button2 with the trailing edge of titleImage, however the width distribution between the two buttons becomes skewed at this point, as demonstrated below.
My objective is to understand what constraints are missing and should be applied to the two buttons to maintain equal widths regardless of device. If possible I would like to achieve this through interface builder as opposed to additional code.
Add the following constraints
Assign equal width from button1 to button2.
Assign horizontal spacing between both buttons.
Assign leading space from button1 to its superview.
Assign trailing space from button2 to its superview.
Assign top space to both the buttons.
Let me know if it works for you.
Follow Steps and Screenshots for easy solution
Step-1)
For Button 1 : Set Constraints: (1)Leading, (2)Top or Bottom asper your need, (3)Height
Step-2)
For Button 2 : Set Constraints: (1)Trailing,(2)Top or Bottom asper your need, (3)Height
Step-3)
Press Ctrl + Drag From Button 1 to Button 2
Select Horizontal Spacing
Step-4)
Select Both Button (using Command) and Add Constraints Equal Width
OUTPUT
Hope it Helps you :)
Stack layout in iOS9, will do the job really nice.
Add stack view to your view and configure as follow:
You can check my example - you can easy aim this, by using proportional constraint.
Also you can easy aim proportional multiple UIViews.
Please look attached example
https://dl.dropboxusercontent.com/u/48223929/TestViewController.xib
My solution is to
Put a small view in the middle of two buttons and make it centre(Horizontal centre in container and vertical centre in contener as 0).
Add height and width to the small view.
Add buttons the constraints and give horizontal space constraints to small view.
Give the small view background colour same as buttons or View's colour.
Note: See the Screenshot.

iOS Auto Layout: Align the edge of one button to the center of another button?

So I have two UIButtons that I'd like to align, but not specifically the edges. I would like to align the (left) edge of one button to the (horizontal) center of another button. So I want it to look like this:
How would I do this AutoLayout in Interface Builder and/or programatically? I hope this isn't too much of a beginner question, but iOS AutoLayout can be so confusing at times...
In IB, create any horizontal constraint between the two buttons. For example, align their centers. Then, select the new constraint object and the Size inspector. There, you can change which attributes are aligned. Select the leading edge of the top button. Now the CenterX of the bottom button is aligned to the leading edge of the top button.

Keep UIButtons centered using autolayout in storyboard

I have two buttons contained within a view (to be precise, a UITableView footer):
I would like to keep the buttons centered (with the gap in-between) regardless of the width of the superview (which will increase, for example, if the device is rotated to portrait orientation).
Is there a way to define these autolayout constraints purely in the storyboard, or will I need to use NSLayoutConstraints in code?
You can achieve that by adding a constraint for each button to align the leading/trailing to the center of the container view, plus some separation between them.
How to do it:
For the left side button, add a constraint between the button and superview to center it horizontally.
Edit the constraint and change the "Second Item" attribute to be button1.trailing, and set a constant of 10 (or whatever distance you want between the right border of the button and the center of the screen).
For the right side button, add a constraint between the button and superview to center it horizontally.
Edit the constraint and change the "Second Item" attribute to be button2.leading and set a constant of -10 (or whatever distance you want between the left border of the button and the center of the screen).

Resources