如何根据 objective c 中的内容调整 textview 和工具栏的大小

How to resize the textview along with toolbar according to the content in objective c

我使用了下面的代码,但是没有得到结果

CGRect newFrame = _text_view.frame;
CGRect newToolbarFrame = self.navigationController.toolbar.frame;
newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);

根据上面的代码,请告诉我什么是新尺寸?

为了根据内容向上扩展textview的高度,我使用了this gitHub项目。

要简单地更改文本视图的位置,使其保持在键盘上方,试试这个

// First add these
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

然后在这些方法中,这样做

- (void) keyboardWillShow:(NSNotification *)note {

    //Get keyboard size and loctaion
    CGRect keyboardBounds;
    [[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];

    // get a rect for the textView frame
    CGRect containerFrame = _text_view.frame;

    // Provided you keep fixed number of maximum lines
    containerFrame.origin.y = self.view.bounds.size.height - (keyboardBounds.size.height + containerFrame.size.height);

    // set views with new info
    _text_view.frame = containerFrame;

    // Change toolbar location as per it's height
    _tool.frame=CGRectMake(0, 
                           _text_view.frame.origin.y - _tool.frame.size.height, 
                           _tool.frame.size.width,
                           _tool.frame.size.height);

}

- (void) keyboardWillHide:(NSNotification *)note{

    CGRect keyboardBounds;
    [[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];

    // get a rect for the textView frame
    CGRect containerFrame = _text_view.frame;
    containerFrame.origin.y = self.view.bounds.size.height - containerFrame.size.height;

    // set views with new info
    _text_view.frame = containerFrame;

    // Similarly again change toolbar location
}

或者您可以简单地将 textview 保留在 scrollView 中,并在键盘出现时更改滚动视图的插图。