简体   繁体   中英

UITextView - changing frame adds right padding

I have a UISlider which can be used to resize a UITextView .
Before resizing the textview , everything looks ok, but after changing its frame for several times, it adds a padding to the right.

Before:
调整大小之前
After:
调整大小后

The textview ( _tvInput ) is the one on the top, and it's not placed in any other view container.

Here's the code that I use for changing its frame:

-(IBAction)widthChanged:(UISlider *)sender{
    CGRect frame = _tvInput.frame;
    frame.size.width = sender.value;
    frame.size.height = MIN(_tvInput.contentSize.height, [self tvMaxHeight]);
    if (sender.value<=_tvInput.superview.frame.size.width-20) {
        frame.origin.x = (_tvInput.superview.frame.size.width-frame.size.width)/2;
    }
    _tvInput.frame = frame;
}

So, any ideas what causes this, and more importantly how it can be fixed?
I tried resetting the contentInset of the textView , but that doesn't seem to be the problem. I also tried setting the contentSize manually, but no luck with that either.

Oh man.. so I managed to fix it by changing this line (notice the conversion from float to int):

frame.size.width = (int)sender.value;

Not sure why that would cause an issue, by the looks of it the fractional part from changing the width kept on adding to the right padding.

I've gotten bitten a few times by this too. I now use textView.frame = CGRectIntegral(theFrame);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM