2017-01-16 41 views
0

我做了一個示例演示,當我在UITextview上鍵入時,它將展開,但視圖不會展開,這是UITextview的超級視圖。如何使用UITextview增加視圖大小?

代碼

- (void)textViewDidChange:(UITextView *)textView 
{ 
    CGFloat fixedWidth = textView.frame.size.width; 
    CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)]; 
    CGRect newFrame = textView.frame; 
    newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height); 
    textView.frame = newFrame; 
    self.myTextView.scrollEnabled = NO; 

    CGRect viewFrame = self.myCommentView.frame; 
    viewFrame.size.height =self.myCommentView.frame.size.height + 1; 
    viewFrame.origin.y=self.myCommentView.frame.origin.y - 1; 
    self.myCommentView.frame =viewFrame; 
    [self.myTextView layoutIfNeeded]; 
    [self.myCommentView layoutIfNeeded]; 
} 

問題是

觀點不符合UITextview擴大。而如果視圖中展開UITextview並沒有擴張。

圖片

enter image description here

UITextView的約束

enter image description here

故事板

enter image description here 我想都應該是擴大時UITextview擴大。

enter image description here 謝謝

+0

將高度約束關係從'='更改爲'> ='。 –

+0

不工作,給我錯誤 – user7018875

+0

約束不正確。 –

回答

1

聲明UITextViewDelegate委託方法。

@property (strong, nonatomic) IBOutlet UITextView *txtview; 
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *backgroundviewhight; // myview hight constrain. 
@property (strong, nonatomic) IBOutlet UIView *myview; 

_txtview.delegate = self; 



- (void)textViewDidChange:(UITextView *)textView 
{ 
     CGFloat fixedWidth = textView.frame.size.width; 
     CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)]; 
     CGRect newFrame = textView.frame; 
     newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height); 
     textView.frame = newFrame; 
     self.txtview.scrollEnabled = NO; 
     _backgroundviewhight.constant = newFrame.size.height; 
     [self.txtview layoutIfNeeded]; 
     [self.myview layoutIfNeeded]; 
    } 

你的TextView限制像

Your textview constrain

創建的約束NSLayoutConstraint您的MyView的HIGHT約束

Myview constrain

輸出:

Here

+0

嘿在哪裏連接backgroundviewhight,因爲使用IBOutlet – user7018875

+0

其實它現在不工作。 – user7018875

+0

@ user7018875你在tableview裏面使用這個textview嗎? –

0

1.將View約束設置爲Leading,Trailing,Bottom並修復其高度並使高度約束出口爲viewHeightConstraint

2.從superview(UIView)引導,尾隨,底部和頂部的UITextView約束。

的UITextView的

3.In shouldChangeTextInRange委託使用下面的方法

NSString *finalString = [textView.text stringByReplacingCharactersInRange:range withString:text]; 

/********** set width in width of your textview and font for your text in font ********/ 
     CGRect rect = [finalString boundingRectWithSize:(CGSize){width , CGFLOAT_MAX} 
            options:(NSStringDrawingUsesLineFragmentOrigin) 
            attributes:@{NSFontAttributeName:font} 
            context:nil]; 
/** Now set the height for view as you type it will increase according with textView **/ 
    _viewHeightConstraint.constant = rect.size.height + 30; 
0

獲得字符串高度按我的先進經驗,你必須設置頂部限制評論您評論查看查看&設定優先級(低:250)就像下面一樣。

設置優先級:

enter image description here

評論查看約束:

enter image description here

設置TextView的約束:

在didTextChange方法

enter image description here

次要更新:

- (void)textViewDidChange:(UITextView *)textView 
{ 

    CGFloat fixedWidth = textView.frame.size.width; 
    CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)]; 
    if (newSize.height>textView.frame.size.height) { 
     [textView sizeToFit]; 
     textView.scrollEnabled = NO; 
    } 

    [textView layoutIfNeeded]; 

    [self.myCommentView layoutIfNeeded]; 
} 

輸出:

enter image description here

希望這將幫助您設置可調整大小的視圖。