- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在尝试设置 UIButton 的 cornerRadius,但我不知道该怎么做。
如果我这样做:
button.layer.cornerRadius = 5;
效果很好,如果我这样做的话:
button.layer.cornerRadius = 5;
[button setBackgroundColor:[UIColor colorWithPatternImage:radialGradient]];
角不是圆的。
我知道我可以解决这个问题
[button.layer setMasksToBounds:YES];
但我专门寻找不同的解决方案,因为我向按钮添加了一些箭头,如果我将掩码设置为边界,箭头将被屏蔽。
编辑:
radialGradient 是用 func 制作的
+ (UIImage *)getRadialGradientImage:(CGSize)size centre:(CGPoint)centre radius:(float)radius startColor:(UIColor *)startColor endColor:(UIColor *)endColor{
// Initialise
UIGraphicsBeginImageContextWithOptions(size, YES, 1);
// Create the gradient's colours
size_t num_locations = 2;
CGFloat locations[2] = { 0.0, 1.0 };
const CGFloat *component_first = CGColorGetComponents([startColor CGColor]);
CGFloat red1 = component_first[0];
CGFloat green1 = component_first[1];
CGFloat blue1 = component_first[2];
const CGFloat *component_second = CGColorGetComponents([endColor CGColor]);
CGFloat red2 = component_second[0];
CGFloat green2 = component_second[1];
CGFloat blue2 = component_second[2];
const CGFloat components[8] = { red1,green1,blue1,1,red2,green2,blue2,1}; // End color
CGColorSpaceRef myColorspace = CGColorSpaceCreateDeviceRGB();
CGGradientRef myGradient = CGGradientCreateWithColorComponents (myColorspace, components, locations, num_locations);
// Normalise the 0-1 ranged inputs to the width of the image
CGPoint myCentrePoint = CGPointMake(centre.x * size.width, centre.y * size.height);
float myRadius = MIN(size.width, size.height) * radius;
// Draw it!
CGContextDrawRadialGradient (UIGraphicsGetCurrentContext(), myGradient, myCentrePoint,
0, myCentrePoint, myRadius,
kCGGradientDrawsAfterEndLocation);
// Grab it as an autoreleased image
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
// Clean up
CGColorSpaceRelease(myColorspace); // Necessary?
CGGradientRelease(myGradient); // Necessary?
UIGraphicsEndImageContext(); // Clean up
return image;
}
最佳答案
下面是我如何通过代码创建一个按钮并设置它的背景颜色。
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 100, 100,50);
[btn setTitle:@"Hello" forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor colorWithRed:128.0/255.0f green:0.0/255.0f blue:0.0/255.0f alpha:0.7]];
btn.frame = CGRectMake(100.0, 100.0, 120.0, 50.0);//width and height should be same value
btn.clipsToBounds = YES;
btn.layer.cornerRadius = 20;//half of the width
btn.layer.borderColor=[UIColor redColor].CGColor;
btn.layer.borderWidth=2.0f;
[self.view addSubview:btn];
下面是与上述代码相关的按钮的图像
您始终可以使用代码来创建背景和边框所需的颜色。希望这对您有所帮助。
关于ios - 将 cornerRadius 和 setbackgroundimage 设置为 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17401032/
我正在尝试关注 the Fabric.js tutorial到 setBackgroundImage 到我与按钮相关联的几个图像之一,并且变得有些奇怪。基本上,按钮 1 和 3 都很慢/没有响应/需要
导航栏和上面显示的分段控件之间有一条细黑线。我需要尝试删除此行。 导航栏不是半透明的(我不希望这样)。 我已经尝试过此代码,但无法正常工作: @IBOutlet var navBar: UINavig
我正在尝试自定义选项卡栏,并使用以下代码来更改背景。 UIImage *TabBarControllerBackground = [UIImage imageNamed:@"ios7TabBarBac
通常我可以设置 UITableView 的背景图片使用 [self.tableView setBackgroundView:imageView]; 此外,使用 SDWebImage 我可以分两步将图
我在iOS8中通过KVO设置了自定义navigationBar,自定义navigationBar设置了BackgroundImage。 我发现 viewContoller.view.frame.ori
我是 iOS 编程的新手,我正在创建我的第一个应用程序。我一直在尝试使用以下代码来更改导航栏背景图像(这是使用新的 iOS 5 方法): [self.navigationController.navi
我正在使用以下代码: UIImage *buttonImage; if (p.placeImage != nil) { buttonImage = [UIImage imageWithData
- (void)viewDidLoad { [super viewDidLoad]; [self.navigationController.navigationBar setBackgro
我有两个疑问: https://jsfiddle.net/2h5nbrr2/1/ 1。如何在背景图像中插入属性: function backchange(img) { var imag = img
UIImage *gradientImage46 = [[UIImage imageNamed:@"navbar.png"]resizableImageWithCapInsets:UIEdgeInse
像许多开发人员一样,我正在研究 IOS 5 的新功能。 我尝试使用外观代理来自定义我的应用程序。但是在某些方法中我得到了一个错误。 这个有效: [[UINavigationBar appearance
我正在尝试在 UIButton 上的文本顶部获取图像。我已将图像作为背景图像放在 UIButton 上并设置 VerticalAlignment,图像现在位于文本上方 :)像这样: button.im
我们使用 Storyboard添加了 UIBarButtonItem 并且在运行时点击我们想要更改它的图像(功能类似于添加书签/删除书签)。 研究堆栈溢出。很多人建议在 UIBarbuttonItem
我在 UIControlStateNormal 中的所有按钮下面都有一条小黑线。后退按钮和完成按钮不受影响。 这是图片: 看得一清二楚, super 讨厌。以下是我设置所有图像的方式: [[UIBar
所以我创建了一个按钮。我希望它有圆角而不制作圆形的 .png 文件我可以设置背景图片。我可以绕过角落。但是,当我设置背景图像时,圆角消失了。 很久以前我做过一次,但似乎无法让它再次工作。如何做到这一点
我正在尝试使用可调整大小的图像作为按钮背景。 UIImage *image = [UIImage imageNamed:@"btn_prev_secondary"]; UIEdgeInsets edg
我在 Xcode 4.2 Build 4D199 中创建了一个新的测试 iOS5 项目,并尝试如上所述设置 UINavigationBar 的外观,但没有任何反应。 我在viewDidLoad中试过的
当按下某个 Controller 时,我正在更改导航栏的背景图像。我想为这种变化制作动画。我可以使用以下代码这样做: [UIView transitionWithView:self.navigatio
我刚刚切换到 iOS 5,除了自定义导航栏外,我的应用程序中的一切似乎都正常工作。我环顾四周,听从了每个人关于调用新方法 setBackgroundImage: forBarMetrics: 的建议,
我正在尝试为 UIButton 设置图像。 我不知道 setBackgroundImage:Forstate 和 setImage:Forstate 之间的区别: 谁能帮帮我? 谢谢并致以最诚挚的问候
我是一名优秀的程序员,十分优秀!