Ios UITextView-添加子视图-自动布局
问题:Ios UITextView-添加子视图-自动布局,ios,uitextview,autolayout,Ios,Uitextview,Autolayout,问题: self.hazeView.backgroundColor = [UIColor clearColor]; 我已经创建了一个子类UITextView,并添加了一个子视图v1 我正在使用Autolayout,因此我尝试添加约束以定位子视图v1 错误: self.hazeView.backgroundColor = [UIColor clearColor]; 它抛出以下错误: 由于未捕获的异常“nsinternalinconsistenceexception”而终止应用程序,原因:“
self.hazeView.backgroundColor = [UIColor clearColor];
- 我已经创建了一个子类
,并添加了一个子视图v1UITextView
- 我正在使用Autolayout,因此我尝试添加约束以定位子视图v1
self.hazeView.backgroundColor = [UIColor clearColor];
它抛出以下错误:
由于未捕获的异常“nsinternalinconsistenceexception”而终止应用程序,原因:“执行后仍需要自动布局-布局子视图。
尝试次数:
self.hazeView.backgroundColor = [UIColor clearColor];
- 我曾尝试在
中创建约束,但我得到了相同的错误layoutSubviews
- 我的主要目标是在文本视图的底部添加淡入淡出效果
self.hazeView.backgroundColor = [UIColor clearColor];
我建议您使用以下库进行自动布局: 使用此选项添加约束非常容易 创建
UITextView
的子类,并在-(void)didmovetoserview
作为:
感谢@mackworth提出的解决方案建议 为了完整起见,我正在回答这个问题 概述:
self.hazeView.backgroundColor = [UIColor clearColor];
在UITextView
上添加子视图,然后使用Autolayout,似乎有一些问题
解决方案:
self.hazeView.backgroundColor = [UIColor clearColor];
因此,解决方案是将HazeView创建为UITextView
父视图的子视图
步骤:
self.hazeView.backgroundColor = [UIColor clearColor];
UITextView
UITextView
和HazeView
作为子视图添加到同一父视图中HazeView
定位在UITextView的底部
[UIColor clearColor]
UIView
的子类,并将UITextView
和HazeView
放在其中,以便可重用self.hazeView.backgroundColor = [UIColor clearColor];
HazeView
是UIView
- (void)drawRect:(CGRect)rect
{
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor *color1 = [UIColor colorWithRed:1.0 green:1.0
blue:1.0 alpha:0.25];
UIColor *color2 = [UIColor colorWithRed:1.0 green:1.0
blue:1.0 alpha:0.5];
UIColor *color3 = [UIColor colorWithRed:1.0 green:1.0
blue:1.0 alpha:0.75];
NSArray *gradientColors = @[(id) color1.CGColor,
(id) color2.CGColor,
(id) color3.CGColor];
CGFloat gradientLocations[] = {0, 0.50, 1};
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) gradientColors, gradientLocations);
CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
CGGradientRelease(gradient);
}
在这里查看可能更好的方法:谢谢,您的建议导致了最终的解决方案