UITextView - addSubview - Autolayout

autolayout, ios, uitextview

Solution

Thanks to @mackworth for the suggestion which led to the solution

For completeness I am answering it.

Overview:

There seems to be some trouble adding subview on `UITextView` and then using Autolayout.

Solution:

So the solution is to create the HazeView as a subview to the parent view of `UITextView`.

Steps:

- Create a `UITextView`

- Create a HazeView (subclass from UIView)

- Add both `UITextView` and `HazeView` as subview to the same parent view

- Position `HazeView` at the bottom of the `UITextView`

- Ensure that the background colour of HazeView is `[UIColor clearColor]`

- Disable user interaction on `HazeView`

- It is best to create a subclass of `UIView` and put the `UITextView` and `HazeView` inside that so that it can be reusable

Creating HazeView:

self.hazeView.backgroundColor = [UIColor clearColor];

`HazeView` is a subclass of `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);
}

Problem

Problem: - I have created a subclass of `UITextView` and added a subview v1. - I am using Autolayout, so I tried to add constraints for positioning the subview v1. Error: It throws the following error: `Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews.` Attempts Made: - I have tried creating the constraints inside `layoutSubviews` and yet i get the same error Objective - My main objective is to add a fading effect at the bottom of the text view Question: - Is there a better way to attain my objective ? - How do I resolve this error ?

Original source

Related problems