iOS 7 UITextView 垂直对齐
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19468417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS 7 UITextView vertical alignment
提问by Napolux
How is that possible that my editable UITextView
(placed inside a straightforward UIViewController inside a UISplitView
that acts as delegate for the UITextView
) is not showing text from the beginning but after something like 6-7 lines?
怎么可能我的可编辑UITextView
(放置在一个简单的 UIViewController 中UISplitView
,作为 的委托UITextView
)不是从一开始就显示文本,而是在 6-7 行之后显示文本?
I didn't set any particular autolayout or something similar, trying to delete text doesn't help (so no hidden chars or something).
我没有设置任何特定的自动布局或类似的东西,尝试删除文本无济于事(因此没有隐藏字符或其他东西)。
I'm using iOS 7 on iPad, in storyboard looks good... The problem is the same on iOS simulator and real devices. I'm getting mad :P
我在 iPad 上使用 iOS 7,情节提要看起来不错……iOS 模拟器和真实设备上的问题是一样的。我生气了:P
Here's some code. This is the ViewController viewDidLoad()
这是一些代码。这是视图控制器viewDidLoad()
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.itemTextField.delegate = self;
self.itemTextField.text = NSLocalizedString(@"NEWITEMPLACEHOLDER", nil);
self.itemTextField.textColor = [UIColor lightGrayColor]; //optional
}
And here are the overridden functions for the UITextView
I'm using some code I've found on StackOverflow to simulate a placeholder for the view (the same stuff on iPhone version of the storyboard works fine)...
这是UITextView
我使用我在 StackOverflow 上找到的一些代码来模拟视图的占位符的覆盖函数(iPhone 版本的故事板工作正常)...
// UITextView placeholder
- (void)textViewDidBeginEditing:(UITextView *)textView
{
if ([textView.text isEqualToString:NSLocalizedString(@"NEWITEMPLACEHOLDER", nil)]) {
textView.text = @"";
textView.textColor = [UIColor blackColor]; //optional
}
[textView becomeFirstResponder];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
if ([textView.text isEqualToString:@""]) {
textView.text = NSLocalizedString(@"NEWITEMPLACEHOLDER", nil);
textView.textColor = [UIColor lightGrayColor]; //optional
}
[textView resignFirstResponder];
}
-(void)textViewDidChange:(UITextView *)textView
{
int len = textView.text.length;
charCount.text = [NSString stringWithFormat:@"%@: %i", NSLocalizedString(@"CHARCOUNT", nil),len];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
return YES;
}
回答by Andrea
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.
尝试-sizeToFit
通过文本后调用。这个答案对于在 UILabel 中垂直对齐文本很有用。
[更新]
我更新了这个答案 o 使其更具可读性。
问题是从 iOS7 开始,UINavigationController 或 UITabbarController 等容器视图控制器可以更改滚动视图(或从它继承的视图)的内容插入,以避免内容重叠。仅当滚动视图是主视图或第一个子视图时才会发生这种情况。为避免这种情况,您应该通过设置automaticallyAdjustsScrollViewInsets
为 NO 来禁用此行为,或覆盖此方法以返回 NO。
回答by Tanguy G.
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?)
}
回答by Jeff Holliday
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.
这是一个相当普遍的问题,所以我会创建一个简单的 UITextView 子类,以便您可以重新使用它并在 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
我会改用 contentInset,确保优雅地处理 contentSize 大于 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
回答by Kiattisak Anoochitarom
use -observerForKeyPath
with contentSize
KeyPath
使用 -observerForKeyPath
与contentSize
KeyPath
Look some code at My Blog (don't focus on ThaiLanguage)
在我的博客上查看一些代码(不要关注泰语)
http://www.macbaszii.com/2012/10/ios-dev-uitextview-vertical-alignment.html
http://www.macbaszii.com/2012/10/ios-dev-uitextview-vertical-alignment.html
回答by Tom Susel
回答by Mike Gledhill
I had the same issue with iOS 8.1, and none of these suggestions worked.
我在 iOS 8.1 上遇到了同样的问题,但这些建议都没有奏效。
What didwork 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
.
什么做的工作是去到故事板和拖我的UITableView
还是UITextView
,这样,就不再是我的屏幕的第一子视图UIView
。
http://www.codeproject.com/Tips/852308/Bug-in-XCode-Vertical-Gap-Above-UITableView
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
.
它似乎与UIView
嵌入UINavigationController
.
Bug ? Bug ? Did I say "bug" ...?
漏洞 ?漏洞 ?我说“错误”了吗......?
;-)
;-)
回答by Jitendra Kulkarni
Swift version of Tanguy.G's answer:
Tanguy.G 答案的 Swift 版本:
if(UIDevice.currentDevice().systemVersion >= "7.0") {
self.automaticallyAdjustsScrollViewInsets = false; // Avoid the top UITextView space, iOS7 (~bug?)
}
回答by LorikMalorik
Check top content inset of textView in -viewDidLoad
:
检查 textView 中的顶部内容插入-viewDidLoad
:
NSLog(@"NSStringFromUIEdgeInsets(self.itemTextField.contentInset) = %@", NSStringFromUIEdgeInsets(self.itemTextField.contentInset));
Reset it in storyboard if it is not zero
如果它不为零,则在故事板中重置它