Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS 7 UITextView垂直对齐_Ios_Objective C_Ipad_Uitextview_Vertical Alignment - Fatal编程技术网

iOS 7 UITextView垂直对齐

iOS 7 UITextView垂直对齐,ios,objective-c,ipad,uitextview,vertical-alignment,Ios,Objective C,Ipad,Uitextview,Vertical Alignment,我的可编辑的UITextView(放置在作为UITextView代理的UISplitView中的简单UIViewController中)怎么可能不从开头显示文本,而是在6-7行之后 我没有设置任何特定的自动布局或类似的设置,尝试删除文本没有帮助(因此没有隐藏字符或其他内容) 我在iPad上使用iOS 7,在故事板上看起来不错。。。 iOS模拟器和真实设备上的问题是相同的。我快发疯了:P 这里有一些代码。这是ViewControllerviewDidLoad() 下面是UITextView的重写

我的可编辑的
UITextView
(放置在作为
UITextView
代理的
UISplitView
中的简单UIViewController中)怎么可能不从开头显示文本,而是在6-7行之后

我没有设置任何特定的自动布局或类似的设置,尝试删除文本没有帮助(因此没有隐藏字符或其他内容)

我在iPad上使用iOS 7,在故事板上看起来不错。。。 iOS模拟器和真实设备上的问题是相同的。我快发疯了:P

这里有一些代码。这是ViewController
viewDidLoad()

下面是
UITextView
的重写函数,我正在使用在StackOverflow上找到的一些代码来模拟视图的占位符(iPhone版本的故事板上的相同内容也可以)


传递文本后,尝试调用
-sizeToFit
。这个答案可能对我们有帮助。
[更新]
我更新了这个答案,使它更具可读性

问题在于,在iOS7中,容器视图控制器(如UINavigationController或uitabarcontroller)可以更改滚动视图(或从中继承的视图)的内容插入,以避免内容重叠。只有当scrollview是主视图或第一个子视图时,才会发生这种情况。为避免禁用此行为,应将
自动调整滚动视图插入设置为“否”,或覆盖此方法以返回“否”。

检查
-viewDidLoad
中textView的顶部内容插入:

NSLog(@"NSStringFromUIEdgeInsets(self.itemTextField.contentInset) = %@", NSStringFromUIEdgeInsets(self.itemTextField.contentInset));

如果不是零,在情节提要中重置它

我也遇到过同样的问题

通过禁用自动滚动视图插入调整解决了此问题:

if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")){
    self.automaticallyAdjustsScrollViewInsets = NO; // Avoid the top UITextView space, iOS7 (~bug?)
}

将-
observerForKeyPath
contentSize
KeyPath一起使用

在我的博客上看一些代码(不要关注泰国语)


受Kiattisak的启发,我将垂直对齐作为
UITextView
的一个类别来实现,这样您就可以控制传统
UITextView
的垂直对齐


您可以将其作为。

这是一个相当常见的问题,因此我将创建一个简单的UITextView子类,以便您可以重用它并在IB中使用它

我会改用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

我对iOS 8.1也有同样的问题,但这些建议都不起作用

所做的工作是进入故事板,拖动我的
UITableView
UITextView
,使它不再是我屏幕的
UIView
的第一个子视图

它似乎与在
UINavigationController
中嵌入
UIView
有关

臭虫?缺陷我是不是说“臭虫”


;-)

Tanguy.G回答的Swift版本:

if(UIDevice.currentDevice().systemVersion >= "7.0") {
            self.automaticallyAdjustsScrollViewInsets = false; // Avoid the top UITextView space, iOS7 (~bug?)
  }

浅蓝色是UITextVIew的背景色?是的,我在故事板中设置了它,以便在屏幕截图中清晰显示。顺便说一句,即使使用默认的背景色,问题也存在。这与最初的问题“如何垂直对齐UITextView”相比已被严重修改。不起作用。
UITextView
的大小调整为30 x 20像素左右,不显示任何文本。您使用的是属性字符串吗?好的,也许lorik给我指出了正确的方向。插入的原因是UITextField具有scrool功能,因为sotryboard中的iOS7会自动插入每种滚动视图的状态栏+导航栏大小(在UINavController中)。可能在这里,您从顶部放置了一个约束,用于固定“顶部距离”,但您没有取消选择“自动边”选项。因此,请检查此VC的故事板并取消选择调整滚动视图插入选项。
NSStringFromUIEdgeInsets(self.itemTextField.contentInset)={0,0,0,0}
NoLuck.Log
self.itemTextField.contentOffset
self.itemTextField.contentOffset=Null更改字段。contentOffset是唯一对我有效的解决方案。(iOS 7.0)谢谢!这是一个类似故事板属性的“代码方式”!只需确保在VC被释放时删除键值观察者。注意,您经常需要调用布局:
view.layoutIfNeeded()
。这很烦人,而且在iOS中做得非常糟糕。
@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
if(UIDevice.currentDevice().systemVersion >= "7.0") {
            self.automaticallyAdjustsScrollViewInsets = false; // Avoid the top UITextView space, iOS7 (~bug?)
  }