Adding constraints programmatically in UIView with UITextView
You can fix this in one of two ways:
- Change the
constant
to-10
for your bottom and trailing constraints.
or
- Switch the order of the
item:
andtoItem:
values in the bottom and trailing constraints. That is, makeitem: self.view
andtoItem: textView
for the bottom and trailing constraints. This is how it is done if you set the constraints in the StoryBoard.
How to create a UITextView programatically, inside UIView that is added programatically too?
The issue is in the next line of code
textView.center = self.view.center
Get rid of it and you will see the textView in the view hierarchy
UITextField not working with add constraints programmatically
I just run your code, it looks like scrollMainView is not visible in the views' hierarchy. Just change the constraints. Here is the code:
func addScrollMainView() {
scrollMainView = UIView()
scrollView.addSubview(scrollMainView)
scrollMainView.translatesAutoresizingMaskIntoConstraints = false
let leadingConst = scrollMainView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 0)
let trailingConst = scrollMainView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: 0)
let topConst = scrollMainView.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 0)
let bottomConst = scrollMainView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0)
NSLayoutConstraint.activate([topConst,leadingConst,trailingConst,bottomConst])
emailFieldView()
}
BTW, It is not related to your question, but it is better to create bg at the beginning. Just use the code below:
func setDesign(){
setBackgroundImage()
setNavegationBar()
addScrollView()
}
How can I add a fixed view under dynamic uitextview?
Don't use the textViews own scrollView for scrolling.
Instead setup an outer scrollView that contains the textView and your footerView (either by embedding in a stackView or by setting up your own constraints).
Getting text and scrollView resizing right can be tricky.
Please refer to this guide for details.
To summarize:
- Add UIScrollView inside the main view in Storyboard
- Add UIView inside the UIScrollView
- Add UITextView inside the UIView (the view added in step 2)
- Make sure "Scrolling Enabled" of UITextView is unchecked
- Add 4 constraints (leading, trailing, top, bottom) on UIScrollView
- Add 4 constraints (leading, trailing, top, bottom) on UIView (the view added in step 2)
- Add "Width Equally" constraint on UIView (the view added in step 2) and the main view
- Add 5 constraints (leading, trailing, top, bottom, height) on UITextView. After this step you shouldn't get any errors and warnings on constraints.
- Add UITextView height constraint IBOutlet on the ViewController.
@property (nonatomic, weak) IBOutlet NSLayoutConstraint *textViewHeightConstraint;
and connect it in Storyboard- Change the UITextView height constraint programmatically.
self.textViewHeightConstraint.constant = [self.textView sizeThatFits:CGSizeMake(self.textView.frame.size.width, CGFLOAT_MAX)].height;
How can I stop the content size constraints when adding a UILabel as a subview to a UITextView, with programmatic constraints
The reason behind this behavior is that a UITextView is a UIScrollView. That means that if your subview does not have a height/width, your layout is ambiguous, so the layout engine adds the intrinsic size to resolve that issue.
It depends on how or where you want to position your subview in your textview, in that case you need to be more specific about the alignment. Check out the options for content hugging priority if that helps you.
But in the end i think you might be better off to not add a label as a subview, if you can use TextKit's features to style and position the content of your planned label inside your textview.
UITextView disappears when AutoLayout Constraints added
Your problem is in the width and height constraints to superView , you have to change the multiplier of these constraints as according to it , every textView fills the entire screen , or at least to the height constraint , BTW if you set leading and trailing to superView then no need for the width constraint as it will cause a conflict because of the leading & trailing margins , so
1- Remove width constraint
2- Change multiplier of height constraint by clicking it to value less than 1 which means full screen height
BTW: I think UIStackView
is perfect for your current case
UITextView that expands to text using auto layout
The view containing UITextView will be assigned its size with setBounds
by AutoLayout. So, this is what I did. The superview is initially set up all the other constraints as they should be, and in the end I put one special constraint for UITextView's height, and I saved it in an instance variable.
_descriptionHeightConstraint = [NSLayoutConstraint constraintWithItem:_descriptionTextView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0.f
constant:100];
[self addConstraint:_descriptionHeightConstraint];
In the setBounds
method, I then changed the value of the constant.
-(void) setBounds:(CGRect)bounds
{
[super setBounds:bounds];
_descriptionTextView.frame = bounds;
CGSize descriptionSize = _descriptionTextView.contentSize;
[_descriptionHeightConstraint setConstant:descriptionSize.height];
[self layoutIfNeeded];
}
Related Topics
Getting Reference to a Dictionary Value
How to Detect Touches on UIimageview of UItableviewcell in Swift
Create Custom Action in a Class for Use in Interface Builder
Switch Statement Where Value Is Int But Case Can Contain Array
[Core Data]: Threw While Encoding a Value. with Userinfo of (Null)
Does Realm Support Computed Property in Swift
Comma Automatically Being Added to Textfield in Swift
How to Navigate from Initial UIviewcontroller to UIsplitviewcontroller in Swift
Swiftui Custom View Repeat Forever Animation Show as Unexpected
Audiounit Callback and Synchronization: How to Ensure Thread Safety with Gcd
Problem with Frameworks in Command Line Tool
Swiftui Vertically Misaligned Text
Lazy Initialization and Deinit
How to Convert an Array to List in Realm
Why Does a Simple Swift Arithmetic Operation Compile So Slow
Swift Bug? Calling Super Class Method When Subclass with Generic Type
Cannot Convert Value of Type 'Nsmutablearray' to Expected Argument Type '[Skaction]'