HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – UIBarButtonItem上没有阴影/浮雕大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有自定义UIBarButtonItem的问题.当我通过创建自定义UIBarButtonItem时
[[UIBarButtonItem alloc]initWithImage:[UIImage imagenamed:@"FilterIcon.png"] style:UIBarButtonItemStyleBordered target:self action:@SELEctor(filterTouched:)];

生成的按钮没有“浮雕”外观,系统项目通过在其图标后面放置一个半透明的黑色阴影来实现.

在左侧,您可以看到“组织”系统栏按钮项,右键是上面代码的结果.

在资源中创建阴影是徒劳的,因为iOS / Cocoa只使用图像的掩码并丢弃任何颜色信息.

有趣的是,如果我在Interface-Builder中创建条形按钮项,它看起来很好.但是,在我的问题的上下文中,我需要在代码中创建按钮项.

解决方法

James Furey的剧本有Objective-C版本.
- (UIImage *)applyToolbarButtonStyling:(UIImage *)oldImage {
    float shadowOffset = 1;
    float shadowOpacity = .54;
    CGRect imageRect = CGRectMake(0,oldImage.size.width,oldImage.size.height);
    CGRect shadowRect = CGRectMake(0,shadowOffset,oldImage.size.height);
    CGRect newRect = CGRectUnion(imageRect,shadowRect);
    UIGraphicsBeginImageContextWithOptions(newRect.size,NO,oldImage.scalE);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextScaleCTM(ctx,1,-1);
    CGContextTranslateCTM(ctx,-(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,[UIColor colorWithWhite:1 alpha:1].CGColor);
    CGContextFillRect(ctx,imageRect);
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

大佬总结

以上是大佬教程为你收集整理的ios – UIBarButtonItem上没有阴影/浮雕全部内容,希望文章能够帮你解决ios – UIBarButtonItem上没有阴影/浮雕所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。