iOS - CALayer setShadowColor not working - ios

I need to set a bottom shadow to a button. I have the shadow but somehow I cannot change the shadow color. What am I doing wrong? Here is the code:
[self.buttonsBar.layer setShadowColor:[UIColor colorWithRed:227.0f/255 green:233.0f/255 blue:239.0f/255 alpha:1.0f].CGColor];
[self.buttonsBar.layer setShadowOpacity:1];
[self.buttonsBar.layer setShadowRadius:1];
[self.buttonsBar.layer setShadowOffset:CGSizeMake(0, 1)];

Make sure your self.buttonsBar.clipsToBounds = NO;
also make sure your self.buttonsBar.layer.masksToBounds = NO;
first try large radius and offset to make sure you see them then reduce the numbers, sometimes it is not easy to see the shadow depending on the background colors ...

Related

If I add a background color to a UIButton, how do I inset it so the background is smaller?

If I have a UIButton and give it a red background, and the background size is a little too small or too big for my liking (but the tap target size is perfect), is there any way to change the size of them?
Basically the equivalent of adding padding in CSS so that it either takes up more area or less? Purely an aesthetic change.
Say with the background color applied to the button it visually takes up a 100px * 30px area. I want it to be 90px * 25px. Is this possible?
One way to do this, is to set the color of a sublayer of the button rather than the background color of the button itself.
- (void)viewDidLoad {
[super viewDidLoad];
CALayer *sub = [CALayer new];
sub.frame = CGRectInset(self.topButton.bounds, 5, 2.5); // this will make the layer 90x25 for a button that is 100x30
sub.backgroundColor = [UIColor redColor].CGColor;
[self.topButton.layer addSublayer:sub];
}

IOS Button border thickness lssue,why?

UIButton top border appears thicker than the following ,but sometimes correct ,why?
code:
UIImage * sanImage = [UIimage imageNamed:#"product_bt1_normal"];
[self.saveBtn setBackgroundImage:[sanImage
stretchableImageWithLeftCapWidth:sanImage.size.width/3
topCapHeight:sanImage.size.height/3] forState:UIControlStateNormal];
Are you trying to make a button? If so, perhaps use a UIButton instead? You can control the border with button.layer.borderWidth = 1.0f
If you're set on using an image, create a UIImageView, and modify the border thickness that way:
UIImageView *iv = [[UIImageView alloc] initWithImage:sanImage];
[iv.layer setBorderWidth:0.5f];
It could be because of off-pixel boundaries. Since you are using height/3.0f, your image is maybe not returning a well-behaved image.
Also, there is a new stretchable image method you should be using, resizableImageWithCapInsets:.
So try this code out:
[self.saveBtn setBackgroundImage:[sanImage resizableImageWithCapInsets:UIEdgeInsetsMake(3.0f, 3.0f, 3.0f, 3.0f)] forState:UIControlStateNormal];
You might need to mess with the values for the insets a bit, I don't know the dimensions of your button image.

How to set shadow gradient image on UIImageView iOS

Hey I'm new to iPhone and I have been trying to set shadow for UIImageView using shadow gradient image i.e. "Image-Shadow.png" using below code.
imageView.layer.shadowColor = [UIColor colorWithPatternImage:[UIImage
imageNamed:#"Image-Shadow.png"]].CGColor;
imageView.layer.shadowOffset = CGSizeMake(0, 1);
imageView.layer.shadowOpacity = 1;
imageView.clipsToBounds = NO;
imageView.layer.shouldRasterize = YES;
My problem is, i am not getting shadow in my UIImageView using above code. Can you please tell me that is this the correct way to add shadow image in UIImageView or it is possible with some other way?
Problem is with the image. I don't think you can provide a pattern image to create a shadow!
Give a proper color like [UIColor blackColor].CGColor and it should work.
The problem is with this line:
imageView.layer.shadowOffset = CGSizeMake(0, 1);
where you are setting the shadow, but not giving it a visible space to display itself.
Positive or negative values should be given to both parameters.
Positive values stretch "down" and "to the right" of your view, negative values stretch "to the left" and "to the up" of your view.
here is my solution as per my requirement :
//set shadow gradient on image view
UIView *shadowView = [[UIView alloc] initWithFrame:imageView.frame]; //add view behind the image view
shadowView.layer.contents = (id) [UIImage imageNamed:#"Image-Shadow.png"].CGImage;
[imageView addSubview:shadowView];
Thanks to all.

UITextField Set Border

I want to make my login interface a little bit more fancy. So I wanted to build somethin like this:
Thats what i Have to far:
self.usernameTextField.layer.borderWidth = 1.0f;
self.usernameTextField.layer.borderColor = [[UIColor colorWithRed:171.0/255.0 green:171.0/255.0 blue:171.0/255.0 alpha:1.0] CGColor];
self.usernameTextField.layer.cornerRadius = 5;
self.usernameTextField.clipsToBounds = YES;
self.passwordTextField.layer.borderWidth = 2.0f;
self.passwordTextField.layer.borderColor = [[UIColor colorWithRed:171.0/255.0 green:171.0/255.0 blue:171.0/255.0 alpha:1.0] CGColor];
self.passwordTextField.layer.cornerRadius = 5;
self.passwordTextField.clipsToBounds = YES;
How can I connect them? There is still a gap between the two UITextFields..
Put plain image background like your interface and put two text fields on that image and set boarder style like below.
[yourTextField setBorderStyle:UITextBorderStyleNone];
The example you show is almost certainly using a table view. If you want to mimic it created a grouped table view with 2 static rows and add your text fields to each row. Make sure the text fields border style is set to none (it should be the default in IB), then set your placeholder text and color. If you want the icons, that is probably just a small UIImageView to the left of the text field.
You can set your UITextField border style to none.
[self.textField setBorderStyle:UITextBorderStyleNone];
and then use custom background images, to get the desired look.
Create your both TextField custom type. Dont set any border style.. Fix the image in ImageView and then place the two text field frame for the text in imageview.

How to get transparency in certain areas of UIView subclass with drawRect override

I've got a UIView subclass with drawRect overridden. The drawing code does not fill the whole area of the view; some of it is meant to be transparent. But when it draws onscreen, what should be transparent is black. How do I get it to be properly transparent?
If I set the backgroundColor to [UIColor clearColor], the whole view disappears. If I set it to any other color, the black area changes to that color. Setting opaque to NO has the same effect. If I draw a transparent rectangle at the start of drawRect like below:
[[UIColor clearColor] setFill];
UIRectFill( rect );
...then it goes to black again. I can use another color in the above code and it draws that color. If I make the color I use partially transparent, I can see the black showing through.
UPDATE: It may be relevant that my view has transparency gradients. I tried testing with a really simple UIView subclass that just draws a blue rectangle inside its bounds and leaves everything else transparent. Setting that view to opaque=NO renders as expected.
The view's opaque has to be set to NO and backgroundColor to [UIColor clearColor]. Also, the wrong blend mode was being used. We had:
CGContextSetBlendMode(context, kCGBlendModeSourceAtop);
We needed:
CGContextSetBlendMode(context, kCGBlendModeDestinationOver);
The drawing code was generated by a graphics tool that can output code, and their support department figured out that the wrong blend constant was being generated because of an off-by-one error.
Try using this line of code to initizlize your view.
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setOpaque:NO];
[self.layer setOpaque:NO];
}
return self;
}
and you can set alpha property to make transparent effect
self.alpha=0.3f;

Resources