作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
自定义 UIBarButtonItem 有问题。当我通过
创建自定义 UIBarButtonItem 时 [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"FilterIcon.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(filterTouched:)];
生成的按钮没有“浮雕”外观,系统项目通过在其图标后面放置一个半透明的黑色阴影来实现。
在左侧您可以看到“组织”系统栏按钮项,右侧是上面代码的结果。
在资源中创建阴影是徒劳的,因为 iOS/Cocoa 只使用图像的 mask 并丢弃任何颜色信息。
有趣的是,如果我在 Interface-Builder 中创建条形按钮项目,它看起来不错。但是,在我的问题上下文中,我需要在代码中创建按钮项。
最佳答案
James Furey 的脚本有 Objective-C 版本。
- (UIImage *)applyToolbarButtonStyling:(UIImage *)oldImage {
float shadowOffset = 1;
float shadowOpacity = .54;
CGRect imageRect = CGRectMake(0, 0, oldImage.size.width, oldImage.size.height);
CGRect shadowRect = CGRectMake(0, shadowOffset, oldImage.size.width, oldImage.size.height);
CGRect newRect = CGRectUnion(imageRect, shadowRect);
UIGraphicsBeginImageContextWithOptions(newRect.size, NO, oldImage.scale);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -(newRect.size.height));
CGContextSaveGState(ctx);
CGContextClipToMask(ctx, shadowRect, oldImage.CGImage);
CGContextSetFillColorWithColor(ctx, [UIColor colorWithWhite:0 alpha:shadowOpacity].CGColor);
CGContextFillRect(ctx, shadowRect);
CGContextRestoreGState(ctx);
CGContextClipToMask(ctx, imageRect, oldImage.CGImage);
CGContextSetFillColorWithColor(ctx, [UIColor colorWithWhite:1 alpha:1].CGColor);
CGContextFillRect(ctx, imageRect);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
关于ios - UIBarButtonItem 上没有阴影/浮雕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11083335/
我正在努力使我的标题具有漂亮的浮雕外观。它在 Chrome 中运行良好,但 Firefox 退出了。我怎样才能使这种效果在两者中都起作用?这是 fiddle : https://jsfiddle.ne
自定义 UIBarButtonItem 有问题。当我通过 创建自定义 UIBarButtonItem 时 [[UIBarButtonItem alloc]initWithImage:[UIImage
在我的 iPhone 应用程序中, 我有一个带有 View 的 ViewController。 请看图片。 这是一个模型。我想要这种弹出式的效果。白色 View 从浅灰色主视图中弹出。我认为这与它的边
我是一名优秀的程序员,十分优秀!