I am making a subclass of UIViewController which, when the user pressed a button, will initiate and add a view from another UIViewController subclass.
Inside the added view I have an instance of UIWebView and UIButton (for closing the popup).
Since it is intended as a pop up, I want to add a border and a shadow to the UIWebView, but since it is nib initiated, I don't know how I can modify the drawing code.
Any help? :)
Take a look at using the view's CALayer.
To add a border:
myView.layer.borderWidth = 1.f;
myView.layer.borderColor = [UIColor blackColor].CGColor;
There are similar methods for adding a shadow:
myView.layer.shadowColor = [UIColor blackColor].CGColor;
myView.layer.shadowOffset = CGSizeMake(4.f, 4.f);
myView.layer.shadowRadius = 4.f;
myView.layer.shadowOpacity = 0.5f;
myView.layer.shouldRasterize = YES;
You will need to add the Quartz framework to your target, and import the header in your controller's .m file:
#import <QuartzCore/QuartzCore.h>
You can add Border and Shadow to any control in this way. You can also set Width of Border and can also make it Rounded.
CALayer * l1 = [viewPopup layer];
[l1 setMasksToBounds:YES];
[l1 setCornerRadius:5.0];
// Add a border
[l1 setBorderWidth:5.0];
[l1 setBorderColor:[[UIColor darkGrayColor] CGColor]];
// Add a shadow
[l1 setShadowColor:[[UIColor darkGrayColor] CGColor]];
[l1 setShadowOpacity:5.0];
// You can more methods for shadow
Just replace viewPopup with your control.
Note:- Don't forget to import <QuartzCore/QuartzCore.h>
Related
I know there are similar questions out there, but haven't found an answer that has helped me resolve my issue. If you've seen one, feel free to mark this as duplicate, but please make sure it answers the entire question and not just similar parts.
I have a UIImageView, and I am rounding the corners and adding a border to it. However, I'm starting to add some drop shadows to give my UI some depth. I'm adding a shadow to my profile picture, and the following code works great for a perfectly square image, which gets cropped into a perfect circle. However, for non-square rectangular images, the shadow is applied to the image itself rather than the image view, so I end up with a round border around a cropped rectangular image, which has a shadow around the edges that don't reach the circular border. Any insight into how I can modify my code to apply this shadow to be around the border, instead of the image within the UIImageView?
Thanks
//Set profile image view to be rounded (default appears rounded due to alpha background, need rest to appear rounded as well
[self.profileImageView layoutIfNeeded];
CALayer *imageLayer = self.profileImageView.layer;
[imageLayer setCornerRadius:self.profileImageView.frame.size.width/2];
[imageLayer setBorderWidth:1];
[imageLayer setBorderColor:[[UIColor colorWithRed:78.0/255.0 green:82.0/255.0 blue:85.0/255.0 alpha:1] CGColor] ];
[imageLayer setMasksToBounds:YES];
//Apply shadows to necessary views for the job badge
[self.profileImageView.layer setShadowColor:[[UIColor blackColor] CGColor]];
[self.profileImageView.layer setShadowRadius:4.0f];
[self.profileImageView.layer setShadowOffset:CGSizeMake(0, -3)];
[self.profileImageView.layer setShadowOpacity:0.5f];
The problem is that shadow is masked together with other content when you set cornerRadius.
You can use an intermediate conteiner view with shadow and masked imageView inside. Here is the code, but you can do the same with Interface Builder and autolayouts as well:
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
[containerView.layer setShadowColor:[[UIColor blackColor] CGColor]];
[containerView.layer setShadowRadius:4.0f];
[containerView.layer setShadowOffset:CGSizeMake(0, -3)];
[containerView.layer setShadowOpacity:0.5f];
[self.view addSubview:containerView];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:containerView.bounds];
imageView.image = [UIImage imageNamed:#"rgb"];
[imageView.layer setCornerRadius:imageView.frame.size.width/2];
[imageView.layer setBorderWidth:1];
[imageView.layer setBorderColor:[[UIColor colorWithRed:78.0/255.0 green:82.0/255.0 blue:85.0/255.0 alpha:1] CGColor] ];
[imageView.layer setMasksToBounds:YES];
[containerView addSubview:imageView];
Output:
I need to set the length size of a UIButton border. I tried using this
[[jb layer] setBorderLength:2.2f];
but got a error saying "setBorderLength is not a method".
Here's my code for my UIButton border:
[[jb layer] setBorderWidth:2.2f];
[[jb layer] setBorderColor:[UIColor blackColor].CGColor];
setting a layer border parameters :
jb.layer.borderColor = [UIColor blackColor].CGColor;
jb.layer.borderWidth = 1;
jb.layer.cornerRadius = jb.bounds.size.width * 0.1;
There is no border length. The border width determines how thick your border is.
The border is around your frame. If you mean your border to be appear wider around your button consider changing the frame.
You would have to create layers and add it to the button to handle this scenario
CALayer * layer = [CALayer layer];
layer.bounds = CGRectMake(0, 0, buttonWidth, 1.0);
layer.backgroundColor = [UIColor blackColor].CGColor;
layer.opacity = 0.1f;
[self.button.layer addSublayer:layer];
This will add border on only the top border of button.
Change the frame and add 3 more layers to cover left right and bottom
I am trying to only add a border to the bottom of my .xib view. I currently have the following code which places a red border on ALL FOUR sides. I only want the border to be on ONE side, the bottom... How do I set the border to the bottom?
Current Code for class that controls .xib:
- (void)awakeFromNib {
self.contentView.layer.borderWidth = 5.0f;
self.contentView.layer.masksToBounds = YES;
self.contentView.layer.borderColor = [UIColor redColor].CGColor;
}
This is what I get:
CALayer *bottomBorder = [CALayer layer];
bottomBorder.borderColor = [UIColor redColor].CGColor;
bottomBorder.borderWidth = 1;
bottomBorder.frame = CGRectMake(0, self.contentView.frame.size.height - bottomBorder.borderWidth, self.contentView.frame.size.width, bottomBorder.borderWidth);
[self.contentView.layer addSublayer:bottomBorder];
If you don't know how to make a view that draws a red border at its bottom, then why not just make a red subview and pin it to the bottom?
After setting border width and corner radius of UIView getting black circular line
Code:
afterSelectFrameView.layer.borderWidth=[slide value];
afterSelectFrameView.layer.cornerRadius=[slide value];
Apply mask to bounds
[imageView.layer setMasksToBounds:YES];
[imageView.layer setCornerRadius:5.0];
setting the border is not necessary, but here is an option for you.
[imageView.layer setBorderWidth:0.0f];
I use the code below (it is part of a ViewUtilities library) to round the corners on any view.
You could modify it to add parameters for other radii, colors, etc.
+(void)AdjustViewEdges:(UIView*)view
{
[view.layer setBorderColor: [[UIColor blackColor] CGColor]];
[view.layer setBorderWidth: 1.0];
[view.layer setCornerRadius:12.0f];
[view.layer setMasksToBounds:YES];
view.backgroundColor = [UIColor colorWithRed:.99 green:0.99 blue:0.99 alpha:1.0];
}
// You will need this as well
#import <QuartzCore/QuartzCore.h>
I don't believe it is giving me the lines you are seeing...but my background colors were different so they may be hidden...
Was this helpful?
Use it like this
afterSelectFrameView.layer.borderWidth=0.0;
afterSelectFrameView.layer.cornerRadius=[slide value];
Happy coding..........
I am using a uitableview and scrollview in a uiview. How to set a border for table or scrollview?
#import "QuartzCore/QuartzCore.h"
then in viewDidLoad use,
tableView.layer.borderWidth = 2.0;
Note
You can also set the border color:
tableView.layer.borderColor = [UIColor redColor].CGColor;
It should work for you. :)
This works quite well for me:
CALayer *layer = _tableView.layer;
[layer setMasksToBounds:YES];
[layer setCornerRadius: 4.0];
[layer setBorderWidth:1.0];
[layer setBorderColor:[[UIColor colorWithWhite: 0.8 alpha: 1.0] CGColor]];
The rounded borders look a little more iOS.
if you want to give the border to tableview with color, use below code
for swift 3
yourTableViewName.layer.borderColor = UIColor.gray.cgColor
yourTableViewName.layer.borderWidth = 1.0