Dynamic expand UITextView on ios 7

2019-02-03 06:52发布

I am using this code

CGRect frame = self.mytext.frame;
frame.size.height = self.mytext.contentSize.height;
self.mytext.frame = frame;

But it doesn`t work in iOS 7. Does anyone know why or have the same problem?

EDITED:

Sorry. I made an UIView, a UITextView and a UIScrollView so i can load the text in my uitextview expand.

I used this code in viewDidLoad

CGRect frame = self.moreDetailsTextView.frame;
frame.size.height = self.moreDetailsTextView.contentSize.height;
self.moreDetailsTextView.frame = frame;

CGRect moreViewFrame =  self.moreDetailsView.frame;
moreViewFrame.size.height = frame.size.height + 270;
self.moreDetailsView.frame = moreViewFrame;

int scrollViewHeight =  moreViewFrame.size.height + 235;
self.scrollView.contentSize = CGSizeMake(320, scrollViewHeight);

This was working fine in ios6, but now with xcode5 and ios7 the uitextview does not expand when i test the app in the simulator.

9条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-02-03 07:17

Noticed in IOS7 sizeToFit wasn't working also - perhaps the solution may help you too

[UITEXTVIEW sizeToFit];
[UITEXTVIEW layoutIfNeeded];
查看更多
太酷不给撩
3楼-- · 2019-02-03 07:18

GrowingTextViewHandler is an NSObject subclass which resizes text view as user types text. Handling resize via NSObject subclass is more useful rather than subclassing UITextView, because it will work for any subclass of UITextView. Here is the sample usage.

@interface ViewController ()<UITextViewDelegate>

 @property (weak, nonatomic) IBOutlet UITextView *textView;
 @property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightConstraint;
 @property (strong, nonatomic) GrowingTextViewHandler *handler;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  self.handler = [[GrowingTextViewHandler alloc]initWithTextView:self.textView withHeightConstraint:self.heightConstraint];
  [self.handler updateMinimumNumberOfLines:3 andMaximumNumberOfLine:8];
 }

- (void)textViewDidChange:(UITextView *)textView {
  [self.handler resizeTextViewWithAnimation:YES];
}
@end
查看更多
何必那么认真
4楼-- · 2019-02-03 07:20

Neither of answers worked for me. I have editable UITextView that I want to dynamically expand in height as the text grows in multiple lines.

It worked fine for iOS6 but I had problems on iOS7 with scrolling the text view in order to keep the cursor visible.

The solution for me was to put bottom inset for text view on iOS7. Like this:

_textView.contentInset = SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0") ? UIEdgeInsetsMake(0, 0, DEFAULT_FONT_SIZE, 0) : UIEdgeInsetsZero;

Satisfiable results. Good luck.

查看更多
登录 后发表回答