2014-03-25 192 views
5

问题:的UITextView - addSubview - 自动布局

  • 我创建的UITextView子类,并增加了一个子视图V1。
  • 我使用的是Autolayout,所以我试图添加约束条件来定位子视图v1。

错误:

它引发以下错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews.

尝试制造:

  • 我曾尝试创建内部约束,但我得到了同样的错误

目的

  • 我的主要目标是在文本视图的底部添加一个渐变效果

问题:

  1. 有没有更好的方法来创建我的目标是什么?
  2. 如何解决此错误?
+1

看到这里可能更好的方法:http://stackoverflow.com/questions/12845590/applying-cagradient-mask-layer-to-uitextview – mackworth

+0

谢谢你的建议导致了最终的解决方案 – user1046037

回答

3

感谢@mackworth为而导致的解决方案

为了完整性,我回答它的建议。

概述:

似乎有一些麻烦,在UITextView添加子视图,然后使用自动布局。

解决方案:

因此该解决方案是创建HazeView作为一个子视图到的UITextView的父视图。

步骤:

  1. 创建UITextView
  2. 创建HazeView(亚类从UIView的)
  3. 作为子视图相同的父视图
  4. 位置HazeView在添加两UITextViewHazeViewUITextView的底部
  5. 确保背景色HazeView的r是对HazeView
  6. [UIColor clearColor]
  7. 禁用用户交互最好是创建UIView一个子类,并把里面的UITextViewHazeView,以便它可以被重用

创建HazeView:

self.hazeView.backgroundColor = [UIColor clearColor]; 

HazeView是一个子类210

- (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); 
} 
0

我建议你使用以下库自动布局:

https://github.com/jrturton/UIView-Autolayout

这是很容易与此添加约束。

创建的UITextView的子类,并在-(void)didMoveToSuperview 添加约束:

-(void)didMoveToSuperview 
{ 
    [self.subview pinToSuperviewEdges:JRTViewPinBottomEdge | JRTViewPinLeftEdge | JRTViewPinRightEdge inset:0]; 
    [self.subview constrainToHeight:10]; 
}