16

アプリに UITextView があります。新しい文字列が追加されるたびに、コンテンツのオフセットを動的に変更する必要があります。以下のコードは、iOS 6 以前のバージョンでは正常に動作しますが、iOS 7 では動作しません。

CGPoint offset = CGPointMake(0, self.textView.contentSize.height - self.textView.frame.size.height);
[self.textView setContentOffset:bottomOffset animated:YES]; 

理由はありますか?

あなたより。

アップデート

UITextView が contentOffset を適切に設定していないか、何かが台無しになっています。結果は期待どおりではありません (iOS 6 以前のバージョンでの結果)。

また、新しいプロパティ「textContainerInset」を設定しようとしましたが、まだ結果はありません...

self.textView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0)    

アップデート

didScroll デリゲート メソッドでわかるように、コンテンツ オフセットに正の値を渡すと、(0,0) にスクロールします。たとえば、コンテンツ オフセットを (0,35) に設定すると、(0,0) にスクロールします...

4

6 に答える 6

8

私が見つけた解決策はまったくエレガントではありませんが、うまくいきます:

目的のオフセットに設定する前に、UITextView の contentOffset を (0,0) に設定しました。なぜこれが機能するのかはわかりませんが、今のところ、私が見つけた最良の解決策です。

CGPoint bottomOffset = CGPointMake(0, self.textView.contentSize.height - self.textView.frame.size.height);
[self.textView setContentOffset: CGPointMake(0,0) animated:NO]; 
[self.textView setContentOffset:bottomOffset animated:YES]; 
于 2013-10-02T12:42:43.453 に答える
5

ふと気づいたのが今日。私の場合、問題は明らかにビューの最初のコントロールである Text View に関連しています。ドキュメント アウトラインの 2 番目の場所 (たとえば、UILabel の後) に移動すると、オフセットがなくなりました。

おそらくこれは、iOS7 のナビゲーション バーの新しい動作のため、意図的に行われています。

于 2014-03-10T10:11:16.530 に答える
4

わかりました、私の調査と他の回答に基づいて、問題はUITextViewコンテンツの高さであり、特定のオフセットへのスクロールではありませんでした。これは、iOS 7 で機能するはずの方法で機能するソリューションです。

UITextViewまず、次のように再作成する必要があります。

    NSString *reqSysVer = @"7.0";
    NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
    BOOL osVersionSupported = ([currSysVer compare:reqSysVer  options:NSNumericSearch] != NSOrderedAscending);

    if (osVersionSupported) {

        NSLog(@"reset chatoutput");

        CGRect outputFrame = self.chatOutput.frame;

        [chatOutput removeFromSuperview];
        [chatOutput release];
        chatOutput = nil;

        NSTextStorage* textStorage = [[NSTextStorage alloc] init];
        NSLayoutManager* layoutManager = [NSLayoutManager new];
        [textStorage addLayoutManager:layoutManager];
        NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:self.view.bounds.size];
        [layoutManager addTextContainer:textContainer];
        chatOutput = [[UITextView alloc] initWithFrame: outputFrame
                                           textContainer: textContainer];
        // if using ARC, remove these 3 lines
        [textContainer release];
        [layoutManager release];
        [textStorage release];

        [self.view addSubview: chatOutput];
    }

次に、このメソッドを使用してUITextViewコンテンツの高さを取得します。

- (CGFloat)textViewHeightForAttributedText:(NSAttributedString*)text andWidth:(CGFloat)width
{
     UITextView *calculationView = [[UITextView alloc] init];
     [calculationView setAttributedText:text];
     CGSize size = [calculationView sizeThatFits:CGSizeMake(width, FLT_MAX)];

     NSLog(@"size: %f", size.height) ;

     return size.height;
}

これで、コンテンツ オフセットを設定できます。

CGPoint bottomOffset;

bottomOffset = CGPointMake(0, [self textViewHeightForAttributedText: self.chatOutput.attributedText andWidth: self.chatOutput.frame.size.width] - self.chatOutput.frame.size.height);

[self.chatOutput setContentOffset:bottomOffset animated:YES];

アップデート

Apple Documentation でこれを読みましたNSAttributedString

結論として、異なるサイズの異なるフォントを使用する場合は、それらもNSAttributeStringインスタンスに設定する必要があります。そうしないと、返される高さが期待どおりになりません。次のようなものを使用したい場合があります。

    NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject: [UIFont systemFontOfSize: 18.0] //or any other font or size
                                                                forKey: NSFontAttributeName];
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString: currentPost.postMessageText attributes: attrsDictionary];
    frame.size.height = [self textViewHeightForAttributedText: attributedString andWidth: 280.0];
    [attributedString release];
于 2013-11-26T11:03:22.147 に答える
2

@vidonismの答えは機能しますが、これはさらに優れています。

// in -viewDidLoad of UIViewController
self.automaticallyAdjustsScrollViewInsets = NO;

この質問に対する受け入れられた回答のトップコメントも参照してください。これは、ViewController のメイン ビューの最初のサブビューである UIScrollView の動作を説明している可能性があります。
これが予想される動作なのかバグなのか、私にはよくわかりません。

于 2014-07-15T23:52:11.610 に答える
0

私が持っていたので、以前の答えはどれもうまくいきませんでした[textView setScrollEnabled:NO]。だから、私はこれで終わった:

[textView setScrollEnabled:YES]
[self.text setContentOffset:CGPointMake(0, page * self.text.frame.size.height) animated:NO];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    [self.text setScrollEnabled:NO];
});
于 2014-10-01T07:21:57.650 に答える
0

私は頼らなければならなかった

[displayText scrollRangeToVisible:NSMakeRange([displayText.text length], 0)];

しかし、それに関する大きな問題は、テキストに追加するたびに、テキストの先頭から始まり、最後までスクロールすることです。Mihai のソリューションもそれを行います。

于 2013-11-14T00:16:48.590 に答える