2011-05-25 41 views
2

我有一個全屏的UITextView,只要出現鍵盤就會變小,這樣鍵盤就不會覆蓋任何文字。作爲其中的一部分,我還更改了textView的底部contentInset,因此當鍵盤出現時文本下方的空間更小,沒有鍵盤時更大。如何阻止UITextView的底邊插入重置爲32?

問題是,無論用戶點擊接近底部的textView開始編輯,底部contentInset自發地將其自身重置爲32.我知道從this answer可以繼承UITextView並覆蓋方法,像這樣:

@interface BCZeroEdgeTextView : UITextView 
@end 

@implementation BCZeroEdgeTextView 

- (UIEdgeInsets) contentInset 
    { 
    return UIEdgeInsetsZero; 
    } 

@end 

但是,這並不能阻止底部插入重置自己 - 它只是改變它重置自己的數字。我怎樣才能讓我的UITextView保持我設置的contentInset值?

回答

1

要使其保持所設置的值,你可以去子類的路線,但回到自己的財產的價值,而不是恆定的,是這樣的:

@interface BCCustomEdgeTextView : UITextView 
@property (nonatomic, assign) UIEdgeInsets myContentInset; 
@end 

@implementation BCCustomEdgeTextView 

@synthesize myContentInset; 

- (UIEdgeInsets) contentInset { 
    return self.myContentInset; 
} 

@end 

但是千萬注意,原因的UITextView將其底部contentInset重置爲32是更標準的插入將切斷自動完成彈出窗口等。

+0

我試過這樣做,但它使得應用程序掛起,一旦我加載視圖。我認爲在調用'contentInset'方法時不能設置屬性。不要擔心自動完成功能 - 我實際上正在嘗試使插圖變大,並且僅在文本未被編輯時。無論如何,當鍵盤出現時,我希望它恢復到32像素。 :) – 2011-05-25 16:13:25

+0

@RicLevy:很奇怪。你可以嘗試通過伊娃而不是屬性來訪問myContentInset,也許會修復它。 – Anomie 2011-05-25 16:44:27

+0

這只是我 - 我試圖返回內置的contentInset屬性,而不是合成我自己的。現在完美運作。謝謝! – 2011-05-26 08:02:10

0

這裏是我的解決方案,但時間長一點:

- (void)setCustomInsets:(UIEdgeInsets)theInset 
{ 
    customInsets = theInset; 
    self.contentInset = [super contentInset]; 
    self.scrollIndicatorInsets = [super scrollIndicatorInsets]; 
} 

- (void)setContentInset:(UIEdgeInsets)theInset 
{ 
    [super setContentInset:UIEdgeInsetsMake(
     theInset.top + self.customInsets.top, 
     theInset.left + self.customInsets.left, 
     theInset.bottom + self.customInsets.bottom, 
     theInset.right + self.customInsets.right)]; 
} 

- (void)setScrollIndicatorInsets:(UIEdgeInsets)theInset 
{ 
    [super setScrollIndicatorInsets:UIEdgeInsetsMake(
     theInset.top + self.customInsets.top, 
     theInset.left + self.customInsets.left, 
     theInset.bottom + self.customInsets.bottom, 
     theInset.right + self.customInsets.right)]; 
} 

子類UITextView,並添加屬性customInsets,每當你需要設置contentInsetscrollIndicatorInsets,設置customInsets代替。