[英]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
.我正在尝试为带有
UILabel
的UITextView
制作一个假占位符。 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.