creating border for uitableview - ios

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

Related

Corner Radius for mask layer drawn on UIView

I am drawing mask path on UIView to make it look like a rectangle at a particular point of view. I want to set corner radius for the mask path so that it can appear as bulged at the side like popover of iOS. Any idea how to implement.
Thanks
Use this Code,
Objective C code:
// Cornor radius
[view.layer setCornerRadius:30.0f];
view.layer.masksToBounds = YES;
// border
[view.layer setBorderColor:[UIColor lightGrayColor].CGColor];
[view.layer setBorderWidth:1.5f];
// drop shadow
[view.layer setShadowColor:[UIColor blackColor].CGColor];
[view.layer setShadowOpacity:0.8];
[view.layer setShadowRadius:3.0];
[view.layer setShadowOffset:CGSizeMake(2.0, 2.0)];
Swift Code:
// Cornor radius
view.layer.cornerRadius = 10.0
view.layer.masksToBounds = true
// border color and width
view.layer.borderColor = UIColor.blueColor().CGColor
view.layer.borderWidth = 2.0
// drop shadow
view.layer.shadowColor = UIColor.blackColor().CGColor
view.layer.shadowOpacity = 1.0
view.layer.shadowRadius = 3.0
view.layer.shadowOffset = CGSizeMake(2.0, 2.0)
hope its helpful

How to draw an outline around a column?

I would like to draw an outline around a column. here is how my app actually looks like: https://gyazo.com/80dffe6a153e82565a610222a43e0c11
So the white column represent the day on where we are actually. But I don't really want to do that.
Actually I'm doing this:
if(column == _intOfTheDay){ //intOfTheDay represent the day one where we are actually
cell.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0.95];
}
I've tried something like this to get closer of the result that I want:
if(column == _intOfTheDay){
cell.layer.borderWidth = 5.0f;
cell.layer.borderColor = [UIColor redColor].CGColor;
}
But the problem here (without all the bugs due to the UICollectionView) is that it draw a rectangle around each cell. And what I would like is to draw only one rectangle but around all the column.
How could I process?
Every help will be appreciated, thanks in advance.
If you don't want to subclass the UICollectionViewCell then use this :
float borderWidth = 5.0;
CALayer *leftBorder = [CALayer new];
leftBorder.frame = CGRectMake(0.0, 0.0, borderWidth, cell.frame.size.height);
leftBorder.backgroundColor = [UIColor redColor].CGColor;
[cell.layer addSublayer:leftBorder];
CALayer *rightBorder = [CALayer new];
rightBorder.frame = CGRectMake(cell.frame.size.width - borderWidth, 0.0, borderWidth, cell.frame.size.height);
rightBorder.backgroundColor = [UIColor redColor].CGColor;
[cell.layer addSublayer:rightBorder];
You can do something like this, set three border to first cell of column- left,right and top and for last cell of column left,right and bottom and for all middle cell left and right. you can use CALayer to set one side border like below example,
CALayer *TopBorder = [CALayer layer];
TopBorder.frame = CGRectMake(0.0f, 0.0f, self.myImageView.frame.size.width, 3.0f);
TopBorder.backgroundColor = [UIColor redColor].CGColor;
[cell.layer addSublayer:TopBorder];

Corner Radius leaving circular line

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..........

UICollectionViewCell with rounded corners AND drop shadow not working

I want my UICollectionViewCells to have rounded corners and drop shadows but I have run into a problem where it seems I can only have one or the other, but not both.
To just round the corners I use this code in the initialization of the cell:
CALayer *layer = [self layer];
[layer setCornerRadius:4];
[layer setRasterizationScale:[[UIScreen mainScreen] scale]];
[layer setShouldRasterize:YES];
To just add a drop shadow I use this code in the initialization of the cell:
CALayer *layer = [self layer];
[layer setMasksToBounds:NO];
[layer setRasterizationScale:[[UIScreen mainScreen] scale]];
[layer setShouldRasterize:YES];
[layer setShadowColor:[[UIColor blackColor] CGColor]];
[layer setShadowOffset:CGSizeMake(0.0f,0.5f)];
[layer setShadowRadius:8.0f];
[layer setShadowOpacity:0.2f];
[layer setShadowPath:[[UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:layer.cornerRadius] CGPath]];
To try and have rounded corners and a drop shadow I use this code in the initialization of the cell:
CALayer *layer = [self layer];
[layer setMasksToBounds:NO];
[layer setCornerRadius:4];
[layer setRasterizationScale:[[UIScreen mainScreen] scale]];
[layer setShouldRasterize:YES];
[layer setShadowColor:[[UIColor blackColor] CGColor]];
[layer setShadowOffset:CGSizeMake(0.0f,0.5f)];
[layer setShadowRadius:8.0f];
[layer setShadowOpacity:0.2f];
[layer setShadowPath:[[UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:layer.cornerRadius] CGPath]];
but this results in the drop shadow only.
Is this a bug or am I doing something wrong?
Works for me great:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
...
cell.layer.masksToBounds = YES;
cell.layer.cornerRadius = 6;
...
return cell;
}
If you place all your subviews into the UICollectionViewCell content view, which you probably are, you can set the shadow on the cell's layer and the border on the contentView's layer to achieve both results.
cell.contentView.layer.cornerRadius = 2.0f;
cell.contentView.layer.borderWidth = 1.0f;
cell.contentView.layer.borderColor = [UIColor clearColor].CGColor;
cell.contentView.layer.masksToBounds = YES;
cell.layer.shadowColor = [UIColor lightGrayColor].CGColor;
cell.layer.shadowOffset = CGSizeMake(0, 2.0f);
cell.layer.shadowRadius = 2.0f;
cell.layer.shadowOpacity = 1.0f;
cell.layer.masksToBounds = NO;
cell.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:cell.bounds cornerRadius:cell.contentView.layer.cornerRadius].CGPath;
Swift 4.0
cell.contentView.layer.cornerRadius = 2.0
cell.contentView.layer.borderWidth = 1.0
cell.contentView.layer.borderColor = UIColor.clear.cgColor
cell.contentView.layer.masksToBounds = true
cell.layer.shadowColor = UIColor.lightGray.cgColor
cell.layer.shadowOffset = CGSize(width: 0, height: 2.0)
cell.layer.shadowRadius = 2.0
cell.layer.shadowOpacity = 1.0
cell.layer.masksToBounds = false
cell.layer.shadowPath = UIBezierPath(roundedRect: cell.bounds, cornerRadius: cell.contentView.layer.cornerRadius).cgPath
There is tricky moment. Cutting corners and dropping shadow is mutually exclusive function in one layer. Dropping shadow is frame extension process, but corners is the process of masking to bounds.
Solution is in function separation. I recommend setup shadow for the cell layer, but cut corners for contentView layer of that cell.
I think I ran into a similar issue. My problem was that clipping in my subviews of the UICollectionViewCell didn't work properly with shadows and rounded borders. The exact same code worked just fine before when I had that view (as a standard UIView subclass though) in a UIScrollView.
So long story short, I moved all this setup from the initWithCoder to a later place after getting it from -dequeueReusableCellWithReuseIdentifier:forIndexPath:. Solved the problem for me. Seems like UICollectionViews are doing something I wouldn't expect to their cells' layers at some point?
If you are using a subclass to make the collection just make sure you do the following.
CALayer *layer = [self layer];
[layer setCornerRadius:_cornerRadius];
[layer setRasterizationScale:[[UIScreen mainScreen] scale]];
[layer setShouldRasterize:YES];
[layer setShadowColor:[[UIColor blackColor] CGColor]];
[layer setShadowOffset:CGSizeMake(0.0,4.0)];
[layer setShadowRadius:6.0f];
[layer setShadowOpacity:0.25];
[layer setShadowPath:[[UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:layer.cornerRadius] CGPath]];
self.contentView.layer.cornerRadius = _cornerRadius;
self.contentView.layer.borderWidth= _borderWidth;
self.contentView.layer.borderColor = _borderColor.CGColor;
self.contentView.backgroundColor = [UIColor whiteColor];
self.backgroundColor = [UIColor clearColor];
works like a charm.

Adding a border and shadow to a nib initiated view

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>

Resources