Adding Constraints Programmatically in UIview with UItextview

Adding constraints programmatically in UIView with UITextView

You can fix this in one of two ways:

  1. Change the constant to -10 for your bottom and trailing constraints.

or


  1. Switch the order of the item: and toItem: values in the bottom and trailing constraints. That is, make item: self.view and toItem: 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

Sample Image

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:

  1. Add UIScrollView inside the main view in Storyboard
  2. Add UIView inside the UIScrollView
  3. Add UITextView inside the UIView (the view added in step 2)
  4. Make sure "Scrolling Enabled" of UITextView is unchecked
  5. Add 4 constraints (leading, trailing, top, bottom) on UIScrollView
  6. Add 4 constraints (leading, trailing, top, bottom) on UIView (the view added in step 2)
  7. Add "Width Equally" constraint on UIView (the view added in step 2) and the main view
  8. Add 5 constraints (leading, trailing, top, bottom, height) on UITextView. After this step you shouldn't get any errors and warnings on constraints.
  9. Add UITextView height constraint IBOutlet on the ViewController. @property (nonatomic, weak) IBOutlet NSLayoutConstraint *textViewHeightConstraint; and connect it in Storyboard
  10. 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



Leave a reply



Submit