简体   繁体   English

在 UITextView 之上添加 UILabel 作为子视图

[英]Adding a UILabel as a subview on top of a UITextView

I am trying to make a fake placeholder for a UITextView with a UILabel .我正在尝试为带有UILabelUITextView制作一个假占位符。 Let's say there's a UITextView inside ViewController and this is what I do:假设ViewController有一个UITextView ,这就是我所做的:

    CGFloat frameWidth = self.postInputField.frame.size.width;
    textViewPlaceholder = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frameWidth, 200)];
    textViewPlaceholder.text = POST_PLACEHOLDER;
    textViewPlaceholder.textColor = [UIColor lightGrayColor];
    [self.view addSubview:textViewPlaceholder];

    [super viewWillAppear:animated];
}

And there's an unnecessary margin on top of the UILabel as shown below: UILabel 顶部有一个不必要的边距,如下所示:

在此处输入图片说明

I could set the y value to be negative something but I want to make sure why this is happening.我可以将y值设置为负数,但我想确定为什么会发生这种情况。 One thing that I do when the view controller is loaded is self.automaticallyAdjustsScrollViewInsets = NO;加载视图控制器时我做的一件事是self.automaticallyAdjustsScrollViewInsets = NO; to get rid of the margin inside the UITextView , but it shouldn't matter.摆脱UITextView内的边距,但这应该无关紧要。

The exact xy values that work are (4, -16) for the UILabel. UILabel 的确切 xy 值是(4, -16)

I think the problem is the height of UILabel :我认为问题是UILabel的高度:

textViewPlaceholder = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frameWidth, 200)];

The height is too large.高度过大。 Try to reduce it.尝试减少它。 And using background color to see more.并使用背景颜色查看更多内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 UILabel 作为子视图添加到 UITextView 时的不明确约束 - Ambiguous constraints when adding UILabel as subview to UITextView UITextView和UILabel子视图属性为零 - UITextView and UILabel Subview Properties are nil 快速将UILabel添加到UITextView - Adding UILabel to UITextView in swift 在UIScrollView上添加UITextView / UILabel - Adding UITextView/UILabel on UIScrollView 将UILabel作为子视图添加到具有编程约束的UITextView时,如何停止“内容大小”约束 - How can I stop the “content size” constraints when adding a UILabel as a subview to a UITextView, with programmatic constraints 在顶部添加 UILabel 作为 UITextField 的子视图 - Add UILabel as subview of UITextField on top iOS - UITextView 边框顶部的 UILabel - iOS - UILabel on top of UITextView border 将UITextView或UILabel作为“ accessoryView”添加到UIAlertView? - Adding a UITextView or UILabel to UIAlertView as “accessoryView”? 添加UILabel作为带有换行符且没有水平指示器的UITextView的子视图 - Add a UILabel as a subview of UITextView with line breaks and no horizontal indicator 子视图(UILabel)放置在超级视图(UITextView)中时不遵守约束 - Subview (UILabel) doesn't respect the constraints when placed in superview (UITextView)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM