简体   繁体   中英

NavigationBar translucent changes UITextView text position

I have a UITextView that is positioning its content at the wrong position. As soon as I enter text, the text becomes invisible, and then jumps to the correct position shortly after (almost correct position, slightly lower than what I expected).

When View Debugging I can see that the _UITextContainerView has the wrong position.

What is strange is when I set my NavigationBar appearance to NO, all works as expected, no problem at all.

UINavigationBar.appearance.translucent = YES;

I have to set my mainView's bound as such else it is not below the "NavigationBar"

    if (self.navigationController.navigationBar.isTranslucent) {
        [[self view] addConstraint:[NSLayoutConstraint constraintWithItem:_mainView
                                                                attribute:NSLayoutAttributeTop
                                                                relatedBy:NSLayoutRelationEqual
                                                                   toItem:self.view
                                                                attribute:NSLayoutAttributeTop
                                                               multiplier:1.0
                                                                 constant:64]];
    } else {
        [[self view] addConstraint:[NSLayoutConstraint constraintWithItem:_mainView
                                                                attribute:NSLayoutAttributeTop
                                                                relatedBy:NSLayoutRelationEqual
                                                                   toItem:self.view
                                                                attribute:NSLayoutAttributeTop
                                                               multiplier:1.0
                                                                 constant:0]];
    }

Before I enter text: 在此处输入图片说明

After Text is entered, the _UITextContainerView moves down, you can see the cursor in the middle of the "_UITextContainerView" view, a tiny more vertically shaped rectangle. 在此处输入图片说明

Just found some more info, that could be related to my problem, my bounds do not match the frame. Still not sure how to fix this. I Think this is related, the distance from 4 to -56 = 60, the height of the NavigationBar.

在此处输入图片说明

Some more info again:

I set the font size on the TextField, and after reducing the font size I notice different results, seem the image.

Did a test by setting translucent off and all(0,1,2,3 characters) is perfect with reduced font size.

See the image for font change results:

在此处输入图片说明

_textField = [UITextView new];
_textField.bounds = CGRectZero;
_textField.backgroundColor = [UIColor whiteColor];    
_textField.layer.cornerRadius = 16;
_textField.layer.borderWidth = 0.5;
_textField.font = [UIFont systemFontOfSize:17.0]; //<< problematic???

If you set navigationBar's translucent to YES, the self.view's origin position will start from the screen's top-left point. If you set NO, it will start from the bar's left-bottom.

Finally after trying several things I found that the by adding this line to my viewDidLoad method solved the problem.

    self.automaticallyAdjustsScrollViewInsets = NO;

A Boolean value that indicates whether the view controller should automatically adjust its scroll view insets. The default value of this property is YES , which lets container view controllers know that they should adjust the scroll view insets of this view controller's view to account for screen areas consumed by a status bar, search bar, navigation bar, toolbar, or tab bar. Set this property to NO if your view controller implementation manages its own scroll view inset adjustments.

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