【发布时间】:2014-02-17 19:11:15
【问题描述】:
这可能看起来有点奇怪,但我有一个自定义的 textView 我正在创建一个清晰的背景。此外,我有一个使用水平彩色线条创建的自定义滚动视图,并将此滚动视图设置在 textView 下方。我不直接在 textView 中添加行的原因(我最初确实这样做)是因为我在顶部 textView 和 scrollView 上方有另一个视图“sandwhiched”。所以层次结构是这样的:Top--->UITextView, Middle--->UIView, Bottom---> UIScrollView.
所有这一切都完美无缺,除了我似乎无法让底部滚动视图与 textView 一起滚动,因为 scrollView 的内容大小与 textView 的不同,因此在 scrollView 中没有绘制额外的行。我什至尝试将scrollView的内容大小设置为textView的大小,但这也不起作用。
如果有任何帮助和提示,我将不胜感激。我将添加用于创建自定义滚动视图的代码。
代码:
LineScrollView.h:
#import <UIKit/UIKit.h>
@interface LineScrollView : UIScrollView
@end
LineScrollView.m:
#import "LineScrollView.h"
@implementation LineScrollView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.backgroundColor = [UIColor clearColor];
self.userInteractionEnabled = NO;
self.scrollsToTop = NO;
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:(95.0f/255.0f) green:(197.0f/255.0f) blue:(236.0f/255.0f) alpha:0.8f].CGColor);
CGContextSetLineWidth(context, 1.2f);
CGContextBeginPath(context);
// Below lineHeight value determined using NSLog on my custom textView
CGFloat lineHeight = 21.473999;
NSUInteger numberOfLines = (self.contentSize.height + self.bounds.size.height) / lineHeight;
CGFloat baselineOffset = 1.2f;
for (int x = 1; x < numberOfLines; x++)
{
CGContextMoveToPoint(context, self.bounds.origin.x, (lineHeight+9)*x - baselineOffset);
CGContextAddLineToPoint(context, self.bounds.size.width, (lineHeight+9)*x - baselineOffset);
}
CGContextClosePath(context);
CGContextStrokePath(context);
}
@end
视图控制器:
#import "TextView.h"
#import "LineScrollView.h"
@property (nonatomic, strong) TextView *TextView;
@property (nonatomic, strong) UIView *sandWhichedView;
@property (nonatomic, strong) LineScrollView *LineView;
- (void)viewDidLoad
{
[super viewDidLoad];
self.sandWhichedView = [[UIView alloc]initWithFrame:initWithFrame:CGRectMake(41.5, 35, 278, 58)];
self.TextView = [[TextView alloc] initWithFrame:CGRectMake(41.5, 35, 278, 58)];
self.LineView = [[LineScrollView alloc]initWithFrame:CGRectMake(41.5, 35, 279, 58)];
self.LineView.delegate = self;
self.TextView.delegate = self;
[self.view addSubview:self.LineView];
[self.view addSubview:self.sandWhichedView];
[self.view addSubview:self.TextView];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView == self.TextView)
{
self.LineView.contentSize = self.TextView.contentSize;
CGPoint offset = self.TextView.contentOffset;
offset.y = self.TextView.contentOffset.y;
[self.LineView setContentOffset:offset];
}
}
【问题讨论】:
标签: ios ios7 uiscrollview uitextview