iPad分组tableView背景颜色?它是什么?

标签 ipad uitableview uicolor

我喜欢 iPad 上 tableView 的新分组背景颜色。我想在我的 UIViewController 的背景上使用相同的颜色,它位于我的分割 Controller 的右侧。

有人知道这个颜色是什么吗?似乎有轻微的渐变。

最佳答案

基于Lutz的回答以及this question的答案,以下自定义 View Controller 代码创建 TableView 背景 View 的副本。然而,自动旋转存在一个问题,这个问题正在下面的第二个代码片段中得到解决。

// You also need to link against QuartzCore.framework
#import <QuartzCore/QuartzCore.h>

- (void) loadView
{
  CGRect mainViewFrame = [self mainViewFrame];
  self.view = [[[UIView alloc] initWithFrame:mainViewFrame] autorelease];
  CAGradientLayer* gradient = [CAGradientLayer layer];
  gradient.frame = self.view.bounds;
  UIColor* startColor = [UIColor colorWithRed:226.0/255.0 green:229.0/255.0 blue:234.0/255.0 alpha:1.0];
  UIColor* endColor = [UIColor colorWithRed:208.0/255.0 green:210.0/255.0 blue:216.0/255.0 alpha:1.0];
  // Cast to (id) is necessary to get rid of a compiler warning
  gradient.colors = [NSArray arrayWithObjects:(id)startColor.CGColor, (id)endColor.CGColor, nil];
  // Inserting at index position 0 ensures that the gradient is drawn
  // in the background even if the view already has subviews or other
  // sublayers
  [view.layer insertSublayer:gradient atIndex:0];

  // add more subviews
}

- (CGRect) mainViewFrame
{
  // add your frame calculating code here
}

- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
  [UIView animateWithDuration:duration
                        delay:0
                      options:UIViewAnimationCurveLinear
                   animations:^{
                     ((CALayer*)[self.view.layer.sublayers objectAtIndex:0]).frame = self.view.bounds;
                   }
                   completion:NULL];
}

上述代码的问题在于,当旋转动画运行时,原来的白色背景会在很短的时间内可见。不幸的是,我对层的了解不够多,无法解决这个问题,所以我开始寻找CAGradientLayer的替代方案。 。设置CALayer.contents我发现了带有渐变图像的内容。

下面的大部分代码涉及创建作为便利构造函数输入所需的图案图像,只是这次渐变是使用 Core Graphics “手动”绘制的,而不是使用 CAGradientLayer 。顺便说一句,渐变绘制代码很大程度上基于 Ray Wenderlich 的 Core Graphics 101 tutorial .

#import <QuartzCore/QuartzCore.h>

- (void) loadView
{
  CGRect mainViewFrame = [self mainViewFrame];
  self.view = [[[UIView alloc] initWithFrame:mainViewFrame] autorelease];
  UIColor* startColor = [UIColor colorWithRed:226.0/255.0 green:229.0/255.0 blue:234.0/255.0 alpha:1.0];
  UIColor* endColor = [UIColor colorWithRed:208.0/255.0 green:210.0/255.0 blue:216.0/255.0 alpha:1.0];

  UIImage* backgroundPattern = [self gradientImageWithSize:CGSizeMake(1, mainViewFrame.size.height)
                                                startColor:startColor
                                                  endColor:endColor];
  self.view.layer.contents = (id)backgroundPattern.CGImage;

  // add more subviews
}

- (CGRect) mainViewFrame
{
  // add your frame calculating code here
}

- (UIImage*) gradientImageWithSize:(CGSize)size startColor:(UIColor*)startColor endColor:(UIColor*)endColor
{
  UIGraphicsBeginImageContext(size);
  CGContextRef context = UIGraphicsGetCurrentContext();

  CGRect rect = CGRectMake(0, 0, size.width, size.height);
  [self drawLinearGradientWithContext:context rect:rect startColor:startColor.CGColor endColor:endColor.CGColor];

  UIImage* gradientImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  return gradientImage;
}

- (void) drawLinearGradientWithContext:(CGContextRef)context rect:(CGRect)rect startColor:(CGColorRef)startColor endColor:(CGColorRef)endColor
{
  CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

  CGFloat locations[] = { 0.0, 1.0 };
  NSArray* colors = [NSArray arrayWithObjects:(id)startColor, (id)endColor, nil];
  // NSArray is toll-free bridged, so we can simply cast to CGArrayRef
  CGGradientRef gradient = CGGradientCreateWithColors(colorSpace,
                                                      (CFArrayRef)colors,
                                                      locations);

  // Draw the gradient from top-middle to bottom-middle
  CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
  CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));

  // Remember context so that later on we can undo the clipping we are going to
  // add to the Core Graphics state machine
  CGContextSaveGState(context);
  // Add clipping with the specified rect so that we can simply draw into the
  // specified context without changing anything outside of the rect. With this
  // approach, the caller can give us a context that already has other stuff
  // in it
  CGContextAddRect(context, rect);
  CGContextClip(context);
  // Finally draw the gradient
  CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
  // Undo clipping
  CGContextRestoreGState(context);

  // Cleanup memory allocated by CGContextDrawLinearGradient()
  CGGradientRelease(gradient);
  // Cleanup memory allocated by CGColorSpaceCreateDeviceRGB()
  CGColorSpaceRelease(colorSpace);
}

我最喜欢这段代码,因为

  • 它自动旋转干净
  • 没有自定义代码来处理自动旋转
  • 与渐变相关的函数可以重构为单独的实用程序类,以使其更具可重用性

关于iPad分组tableView背景颜色?它是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5736515/

相关文章:

iphone - Apple 的 Reachability 类是否有更复杂的替代品?

ios - 每当内容在 LynnBubbleTableView 框架中增长时,如何移动到 tableview 的底部?

uitableview - Swift - UITableView 滚动事件

arrays - 在数组中存储值与在 Swift 中创建扩展函数

ios - 基于进度值的 UIColor 过渡

iphone - iOS部署目标

iphone - 以编程方式捕获屏幕视频

objective-c - 从 UITableView 调用 UIPopoverController - Objective-c

iphone - UITableViewCell 中的多个视频播放问题

iPhone iOS 如何将 UIColor 序列化/保存到 JSON 文件?