2014-03-25 128 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]; 
}