iOS 7 UITextView vertical alignment

前端 未结 8 659
不思量自难忘°
不思量自难忘° 2020-11-29 04:21

How is that possible that my editable UITextView (placed inside a straightforward UIViewController inside a UISplitView that acts as delegate for t

相关标签:
8条回答
  • 2020-11-29 05:01

    Try to call -sizeToFit after passing the text. This answer could be useful to Vertically align text within a UILabel.
    [UPDATE]
    I update this answer o make it more readable.
    The issue is that from iOS7, container view controllers such as UINavigationController or UITabbarController can change the content insets of scroll views (or views that inherit from it), to avoid content overlapping. This happens only if the scrollview is the main view or the first subviews. To avoid that you should disable this behavior by setting automaticallyAdjustsScrollViewInsets to NO, or overriding this method to return NO.

    0 讨论(0)
  • 2020-11-29 05:17

    I had the same issue with iOS 8.1, and none of these suggestions worked.

    What did work was to go into the Storyboard, and drag my UITableView or UITextView so that it was no longer the first subview of my screen's UIView.

    http://www.codeproject.com/Tips/852308/Bug-in-XCode-Vertical-Gap-Above-UITableView

    It seems to be linked to having a UIView embedded in a UINavigationController.

    Bug ? Bug ? Did I say "bug" ...?

    ;-)

    0 讨论(0)
  • 2020-11-29 05:19

    This is a fairly common problem, so I would create a simple UITextView subclass, so that you can re-use it and use it in IB.

    I would used the contentInset instead, making sure to gracefully handle the case where the contentSize is larger than the bounds of the textView

    @interface BSVerticallyCenteredTextView : UITextView
    
    @end
    
    @implementation BSVerticallyCenteredTextView
    
    - (id)initWithFrame:(CGRect)frame
    {
        if (self = [super initWithFrame:frame])
        {
            [self addObserver:self forKeyPath:@"contentSize" options:  (NSKeyValueObservingOptionNew) context:NULL];
        }
        return self;
    }
    
    - (id)initWithCoder:(NSCoder *)aDecoder
    {
        if (self = [super initWithCoder:aDecoder])
        {
            [self addObserver:self forKeyPath:@"contentSize" options:  (NSKeyValueObservingOptionNew) context:NULL];
        }
        return self;
    }
    
    -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
    {
        if ([keyPath isEqualToString:@"contentSize"])
        {
            UITextView *tv = object;
            CGFloat deadSpace = ([tv bounds].size.height - [tv contentSize].height);
            CGFloat inset = MAX(0, deadSpace/2.0);
            tv.contentInset = UIEdgeInsetsMake(inset, tv.contentInset.left, inset, tv.contentInset.right);
        }  
    }
    
    - (void)dealloc
    {
        [self removeObserver:self forKeyPath:@"contentSize"];
    }
    
    @end
    
    0 讨论(0)
  • 2020-11-29 05:20

    Swift version of Tanguy.G's answer:

    if(UIDevice.currentDevice().systemVersion >= "7.0") {
                self.automaticallyAdjustsScrollViewInsets = false; // Avoid the top UITextView space, iOS7 (~bug?)
      }
    
    0 讨论(0)
  • 2020-11-29 05:22

    use -observerForKeyPath with contentSize KeyPath

    Look some code at My Blog (don't focus on Thai Language)

    http://www.macbaszii.com/2012/10/ios-dev-uitextview-vertical-alignment.html

    0 讨论(0)
  • 2020-11-29 05:27

    I got through the same kind of issue.

    Solved it by disabling the automatic scrollView insets adjustement :

    if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")){
        self.automaticallyAdjustsScrollViewInsets = NO; // Avoid the top UITextView space, iOS7 (~bug?)
    }
    
    0 讨论(0)
提交回复
热议问题