Moving UILabel dynamically based on UITextView contents - Using Storyboard - ios

Using storyboard, I am trying to put a UILabel below UITextView. Content of UITextView can grow based on user input. I want my UILabel to reposition its Y value based on UITextView contents. I've tried fixing vertical spacing between UITextView and UILabel but that is not helping.
What else I shall be doing to crack this?

You should use constraints explicitly. The label in particular must have the constraint "top space to Textview" set to N points. Then set an outlet in your view controller from the constraint "top space to Textview" and change that constraint to resize uilabel Y position.
#IBOutlet weak var labelYConstraint: NSLayoutConstraint!
func changeLabelPosition(sender: AnyObject) {
labelYConstraint.constant = newY
}
Let me know if you need more details

Related

How to make two button center view on the center without cross border?

Have a look at the screenshot
I using stackview to stack the textfield, view, and tableview together. When I doing the constraint on the view,
I set the left constraint is 20, and right constraint as 20 on the both button, more over,
I add the width constraint on the both button but ended up the button show the width is different and cannot looks center on the simulator.
How do I make it to be center?
I'm working under the assumption you want your view to look like this:
V: textField - viewWithButtons - tableView
For your buttons, I'd highlight them and make them into a horizontal stack. Under attributes inspector, make the alignment fill, distribution fill equally, spacing 8 (or whatever you want).
From there, click your textFieldView, horizontalStackViewiewWithButtons, and tableView and then turn those into a vertical stack. From there, select your verticalStack from the document outline and click the Pin button at the lower-right corner of the screen. Left and right pins are 0, top pin is "Use Standard Value"
From there, work your way "inward" when you add constraints. The outer stack is mostly taken care of. You'll probably want to add a pin for the height of your textFieldView and your horizontalStackViewWithButtons.
I am also new to ios just my suggestion try this.
In storyboard set width constraint for both buttons
Create references for that constraints in your swift code.
#IBOutlet weak var height1: NSLayoutConstraint!
#IBOutlet weak var height2: NSLayoutConstraint!
In your viewwill appear method calculate width using your screen width for example
let screenSize: CGRect = UIScreen.mainScreen().bounds;
let width = screenSize.width;
//Then you need to remove the constraint spaces so
left(10) - Button1 - middle (10) - Button2 - Right(10)
So toatal 30
let width_available = screenSize.width - 30;
height2.constant = width_available/2
height1.constant = width_available/2
Edit 1:
The real simple solution just set equal width for both buttons from the storyboard.
Also you need horizontal spacing between the buttons

Update constraints of other elements when UITableView height change

I have a UITableView of which I am defining height as 200 in autolayout. Based on that I am laying out other elements below it like UITextField etc. After that in run time I am fetching data from server and populating in UITableView due to which i am updating UITableView's height based on its content size. Following code I am using for it
self.myTableView.frame = CGRectMake(0 , 0, self.myTableView.frame.width, self.myTableView.contentSize.height)
But due to this, all the elements placed below UITableView still appear at same location which they were while laying out in Autolayout. Means change in height of UITableView makes no difference to them. Following image depicts this problem. What could be possible solution for this?
Here you can see, text fields are getting overlapped on tableview at run time. I am using Swift 2 in Xcode 7.2
If you have all required constraints to your table view and other view.
Don't change the frame of TableView to change height of it.
Instead create IBOutlet of height constraint of your tablview.
e.g. say IBOutlet name is constraintTableViewHeight,
then you can the the hight easily.
constraintTableViewHeight.constant = yourNewHeightValue
//update all constraint of your view and its inner view
self.view.layoutIfNeeded();
Refer Image to create IBOutlet for your height Constraint.
Take IBOutlet of NSLayoutConstraint for tableView Height and set its value not set tableview frame it's not working if you are already given constraints for tableview so change tableview hight constant
like if you take tblHeight for tableView height then set tblHeight.constant = self.myTableView.contentSize.height
You have to create an outlet connection for your table view height constraint:
#IBOutlet var heightConstraint: NSLayoutConstraint!
and when you want to change table view height, you can change the constraint value like this:
heightConstraint.constant = newHeightValue
self.view.layoutIfNeeded()

How to use AutoLayout programmatically with subviews inside a UITextView as the text position changes dynamically when the UITextView resizes

I have a UITextView with text and UIImageView inside it. The textView resizes according to the device through the use of auto layout constraints.
But whenever the textView is resized the position of the text inside the UITextView also changes dynamically but the ImageView and Bezier path for the specific image remains constant.
The image is related to the text so I want to place it at a specific position in relation to the text. So how to do it using Auto Layout.
I have shown below what I want to achieve. Both images have different dimensions hence different text alignment.
You have to code some like this example above, for each one object that you wish to align:
#IBOutlet weak var messageTextView: UITextView!
var newConstraint = NSLayoutConstraint.constraintsWithVisualFormat("H:[messageTextView]", options: NSLayoutFormatOptions.AlignAllCenterX metrics: nil, views: ["messageTextView": messageTextView])
messageTextView.addConstraints(newConstraint)

Dynamically Sized UITextView in Storyboard

I would like to add a paragraph of text to a UIView in my storyboard in the form of a non-editable, non-scrolling and non-selectable UITextView. I would like the UITextView to take up a variable number of lines based on how many it needs. It should have a fixed width and y position. How do I accomplish this in the storyboard in such a way that I can link other UI elements' constraints to the bottom of the UITextView?
Make a constraint pinning the text view’s height to some constant, and make an IBOutlet to that constraint so you can access it in code. Whenever the text changes, set the constraint’s constant to the text view’s contentSize.height:
textView.sizeToFit()
textView.layoutIfNeeded()
textViewHeightConstraint.constant = textView.sizeThatFits(CGSize(textView.frame.size.width, CGFloat.max)).height
Editing Zev Eisenberg answer for Swift 3. CGFloat.max doesn't work anymore. There is now CGFloat.greatestFiniteMagnitude
So updated version for Swift 3 is:
#IBOutlet var textViewHeight: NSLayoutConstraint!
textView.sizeToFit()
textView.layoutIfNeeded()
textViewHeight.constant = textView.sizeThatFits(CGSize(width: textView.frame.size.width,
height: CGFloat.greatestFiniteMagnitude)).height
Selected answer is not work for me. Then i found my solution;
1- Uncheck Scrolling Enabled property. Then, make an IBOutlet that constraint and write that code;
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
textViewConstraint.constant = textView.intrinsicContentSize().height
}

Center Align UIImageview and Large UILabel AutoLayout

I have a UIImageView and a UILabel. Label has greater height than image, so I want to center align Y position for both.
Here is original case:
When I apply Center Y from image to label, label content is truncated.
Please can someone guide me for correct constraints... Thanks!
I am using the setup above in my storyboard. A UIImageView on the left and a UITextView on the right; setup in a UIViewController. I setup my constraints like this:
UIImageView
Width Equals: // Whatever you want
Height Equals: // Whatever you want
Align Center Y: Text View
Leading Space to Superview: 16
Trailing Space to Text View: 8
UITextView
Align Center Y: Image View
Trailing Space to Superview: 16
Leading Space to Image View: 8
Top Space to Top Layout Guide: 16
Height Equals: // Whatever you want; this will change
The center Y constraint will keep the UIImageView centered with the UITextView. The top space constraint on the UITextView is what will set the vertical positioning for the views. The height constraint on the UITextView is where the dynamic height portion comes in. Make an outlet to your view controller for the UITextView and the text view height constraint. Also set yourself as the text view's delegate. This will allow us to change everything dynamically as text in the text view is changed.
class ViewController: UIViewController, UITextViewDelegate {
#IBOutlet weak var textView: UITextView!
#IBOutlet weak var textViewHeight: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
self.textView.delegate = self
}
func textViewDidChange(textView: UITextView) {
// Ask the text view how much size it needs to fit its content if it has to
// fit in its current width but can grow vertically as much as it needs
let sizeToFitIn = CGSizeMake(textView.bounds.size.width, CGFloat(MAXFLOAT))
let newSize = self.textView.sizeThatFits(sizeToFitIn)
self.textViewHeight.constant = newSize.height
}
}
I don't use a center constraint if I want to center something because it doesn't work for me always either, as you said. What i do is i add a constraint for leading space and trailing space for X centering and top space and bottom space for Y centering.
That would center your label with 30px space on the left and right side. If you want to use the entire space just make the constant to 0. (For Y just change the other two --> Note to activate the constraint you have to click on the red line)

Resources