ios:how to add top and bottom border in tableview? - ios

I want table like this image that show top and bottom border with color only
i have apply this code in viewDidLoad()
tblFilter.layer.borderWidth = 0.5
tblFilter.layer.borderColor = UIColor.white.cgColor
above code add border at all side that i dont want.
i want table border in only top or bottom side.
i want tableview border like given image. see given image.
please suggest me solution
Thanks

my problem is solved.
i added one custom view. In that view i added two view with 1px height at top and bottom of view and i added table-view in that custom view.

Try this
let topBorder = CAShapeLayer()
let topPath = UIBezierPath()
topPath.move(to: CGPoint(x: 0, y: 0))
topPath.addLine(to: CGPoint(x: tblFilter.frame.width, y: 0))
topBorder.path = topPath.cgPath
topBorder.strokeColor = UIColor.red.cgColor
topBorder.lineWidth = 1.0
topBorder.fillColor = UIColor.red.cgColor
tblFilter.layer.addSublayer(topBorder)
let bottomBorder = CAShapeLayer()
let bottomPath = UIBezierPath()
bottomPath.move(to: CGPoint(x: 0, y: tblFilter.frame.height))
bottomPath.addLine(to: CGPoint(x: tblFilter.frame.width, y: tblFilter.frame.height))
bottomBorder.path = bottomPath.cgPath
bottomBorder.strokeColor = UIColor.red.cgColor
bottomBorder.lineWidth = 1.0
bottomBorder.fillColor = UIColor.red.cgColor
tblFilter.layer.addSublayer(bottomBorder)

If don't want to use the table footer and header you can use the table header view and footer view where you can return view with white color and height would be 1 and width same as table width

Use tableview header and footer.
// in -viewDidLoad
self.tableView.tableHeaderView = ({UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1 / UIScreen.mainScreen.scale)];
line.backgroundColor = self.tableView.separatorColor;
line;
});
Same do it for footer also.
self.tableView.tableFooterView = ({UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1 / UIScreen.mainScreen.scale)];
line.backgroundColor = self.tableView.separatorColor;
line;
});
Or else just add UIVIEW to the background and set the color. That will also help.

Take two UIView. Give it white color.
y cordinate of top uiview is min y of uitableview and y cordinate of bottom uiview is max y of uitableview.
you can get min y and max y using GetMaxY and GetMinY.
width of both uiview are same as uitableview.
height of both uiview are 1px.
hope this will help you.

take label in the cell with height value as 1 and give border to it. Programmatically hide/show on your require index.

Related

Views in circle with animation

What I've to do is to lay out few UIButtons circularly and whenever any button is tapped that button should animate to the top position(top of the circle, i.e. first button in the circle) and hence all the other buttons should animate appropriately so that the tapped button comes to the top.
Now, I've arranged the buttons circularly using
double perAngle = 2 * M_PI / BUTTONS_COUNT;
for (int i = 0; i < BUTTONS_COUNT; i++) {
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(sin(perAngle * i) + basePoint.x, cos(perAngle * i) + basePoint.y, Width, Height)];
[self.View addSubView:btn];
}
I'm rotating this view around z-axis using CAAnimation so that it looks like all buttons are moving in circle when any button is tapped. The problem is I cannot decide by what angle to rotate so that every time only the tapped button comes to the top and not any other button.
Or any other totally different approach to achieve this?
Here is a playground that shows how to distribute the buttons in a circle using the transform (the starting angle is zero so just add whatever the current angle chould be and everythign will move). Whatever is at 270 degrees or 1.5 pi will be on top of the circle. You can animate the transforms to change the angle. Note that I am setting the frames here because its a playground. You can use constraints in your storyboard to achieve the same thing.
let view = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 500))
let buttonCount = 6
for i in 0..<buttonCount {
let button = UIButton()
button.center = view.center
button.setTitle("button \(i)", for: .normal)
button.sizeToFit()
view.addSubview(button)
var transform = CGAffineTransform(rotationAngle: -2 * CGFloat.pi * CGFloat(i) / CGFloat(buttonCount))
.translatedBy(x: 100, y: 0)
.rotated(by: 2 * CGFloat.pi * CGFloat(i) / CGFloat(buttonCount))
button.transform = transform
}
PlaygroundPage.current.liveView = view

How to draw a 1px line in storyBoard?

As we know, the table view separator is so thin and beautiful. Sometimes we have to build some separator line in storyboard or nib, the min number is 1, but actually the line appears much thicker than we expected.
My question is how to draw a 1px line in storyboard?
Xcode 9 Swift: You can add a thin seperator with 1px line in the Storyboard using UIView.
Under Size Inspector, Just set the height to 1, width example to 360.
Apple documentation on UIView.
I got your point too, finally I find way out.
As we know, if we draw a line and set the height by code, we can set the height equal to (1.0 / [UIScreen mainScreen].scale).
But here, you wanna to draw in storyboard.
My way is subclass UIView or UIImageView, based on your demand as OnePXLine. In OnePXLine class, override layoutSubviews like below:
- (void)layoutSubviews {
[super layoutSubviews];
CGRect rect = self.frame;
rect.size.height = (1 / [UIScreen mainScreen].scale);
self.frame = rect;
}
And you can draw 1 px line in storyboard by use this class.
Goodluck!
This would help you if you're coding in Swift:
func lineDraw(viewLi:UIView)
{
let border = CALayer()
let width = CGFloat(1.0)
border.borderColor = UIColor(red: 197/255, green: 197/255, blue: 197/255, alpha: 1.0).CGColor
border.frame = CGRect(x: 0, y: viewLi.frame.size.height - width, width: viewLi.frame.size.width, height: viewLi.frame.size.height)
border.borderWidth = width
viewLi.layer.addSublayer(border)
viewLi.layer.masksToBounds = true
}
You can do like in any UIControl/UIElement for this and change the height as 1
If you want to 1 Point use this - 1
If you want to 1pixel Use this - 1.0 / UIScreen.mainScreen().scale
Objective-C
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, 1)]; // customize the frame what u need
[lineView setBackgroundColor:[UIColor yellowColor]]; //customize the color
[self.view addSubview:lineView];
Swift
var lineView = UIView(frame: CGRect(x: 0, y: 50, width: self.view.frame.size.width, height: 1))
lineView.backgroundColor = UIColor.yellow
self.view.addSubview(lineView)
If you want a more information see this once
you can assign float value for view frame . so what you can do is take any view (lable,textview,view,textfield) and assign height as 0.4f or something programatically and width to match self width.
lable.frame = CGRectMake(0,0,width of self.view,0.4f);
Try it in cellForRowAtIndex
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 1)];
separatorLineView.backgroundColor = [UIColor colorWithRed:242/255.0f green:222/255.0f blue:52/255.0f alpha:1.0];
[cell.contentView addSubview:separatorLineView];
Swift 5 code base on what #childrenOurFuture answered:
class OnePXLine: UIView {
override func layoutSubviews() {
super.layoutSubviews()
var rect = self.frame;
rect.size.height = (1 / UIScreen.main.scale);
self.frame = rect;
}
}

UIActivityIndicatorView Not Centered

I have a TableViewController with a UIActivityIndicatorView as a subview while the content loads and it works like it should. The only problem is that I can't get the activity indicator centered on the screen.
CGFloat width = CGRectGetWidth(self.view.bounds);
CGFloat height = CGRectGetHeight(self.view.bounds);
loadingIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(width / 2, height / 2, 125, 125)];
loadingIndicator.center = CGPointMake(width / 2, height / 2);
loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
loadingIndicator.hidesWhenStopped = YES;
[self.tableView addSubview:loadingIndicator];
[loadingIndicator startAnimating];
Also worth noting, the large indicator seems to be shifted 3 pix left and up. (swift, iOS 9.2.1)
Maybe its because your view is of wrong size in xib(say, its for iPhone 5 in xib and you are running it on iPhone 6 ).
So, your height and width calculation is wrong.
Instead, try using
CGFloat width = CGRectGetWidth(UIScreen.mainScreen.bounds);
CGFloat height = CGRectGetHeight(UIScreen.mainScreen.bounds);
Hope it helps :)
Try the Code :
CGFloat width = CGRectGetWidth(self.tableView.bounds);
CGFloat height = CGRectGetHeight(self.tableView.bounds);
UIActivityIndicatorView *loadingIndicator;
loadingIndicator = [[UIActivityIndicatorView alloc]init];
loadingIndicator.center = CGPointMake(width / 2, height / 2);
loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
loadingIndicator.hidesWhenStopped = YES;
[self.tableView addSubview:loadingIndicator];
[loadingIndicator startAnimating];
Try
[loadingIndicator setCenter:[self.tableView center]];
try by change this line loadingIndicator.center = CGPointMake(width / 2, height / 2); to loadingIndicator.center = self.view.center; and add the subviewloadingIndicator to self.view
Your code is perfect, but I think you set frame on basic of self.view and added that indicator on tableview.
so add that indicator on self.view.
If your problem is that the position is not correct horizontally, it may be that view's bounds is still the freedom width from your storyboard.
Try to put your code in viewDidLayoutSubviews. To avoid repeated adding to the view, set the activityObject as a property of this class.
Tejvansh Singh Chhabra's answer is good when you want to center in the screen. Just when generally if you want to position view based on view's frame or bounds, put code after viewDidLayoutSubviews stage, or add constraints to the view.

Swift add line above to control

In the below screen, I want to add horizontal line above to "Item" label and after "Add" button (below to the add button, I have dynamic table). UIGraphicsGetCurrentContext() doesn't work here.
Can someone help me how to do this?
It is simple to add a subview to act as a line. For example:
Swift 4
var lineView = UIView(frame: CGRect(x: 0, y: 100, width: 320, height: 1.0))
lineView.layer.borderWidth = 1.0
lineView.layer.borderColor = UIColor.black.cgColor
self.view.addSubview(lineView)
Objective C
UIView * lineview = [[UIView alloc] initWithFrame:CGRectMake(0, 100,320,1)];
lineview.layer.borderColor = [UIColor blackColor].CGColor;
lineview.layer.borderWidth = 1.0;
[self.view addSubview:lineview];
Or you can refer to this link to add CALayer or draw a view
how do you draw a line programmatically from a view controller?
You can achieve this in Storyboard.
All you have to do is drag a UIView with height = 1 and width whatever is good for you (ideally equal to screen width). Place it where you want the lines to be.
For Swift 3:
let lineView = UIView(frame: CGRect(x:0,
y: self.yourLabel.height - 1 ,
width: self.yourLabel.frame.width,
height: 1.4
)
)
lineView.backgroundColor = UIColor.blue
self.yourLabel.addSubview(lineView)

Drop a shadow to right and bottom of uiview

I have to drop a shadow to the right and bottom of uiview.Im doing this in interface builder.But I see the shadow dropped to top of it.Tried differnt sizes.but couldn't get it.
layer.masksToBound=No
layer.shadowOpacity=0.15
layer.shadowRadius=2
layer.shadowOffSet={10,-10} //Values being set in Interfacebuilder.
Still this drops shadow at top.What should I do to get at bottom of view.
Try the following code, it might help you
myView.layer.shadowColor = [UIColor purpleColor].CGColor;
myView.layer.shadowOffset = CGSizeMake(5, 5);
myView.layer.shadowOpacity = 1;
myView.layer.shadowRadius = 1.0;
myView.layer.maskToBounds = NO;
I tested this code and it's working and output is:
Hi I have used below code ,it will provide you with shadow you want.
UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:_viewShadow.bounds];
_viewShadow.layer.masksToBounds = NO;
_viewShadow.layer.shadowColor = [UIColor blackColor].CGColor;
_viewShadow.layer.shadowOffset = CGSizeMake(10.0f, 5.0f); /*Change value of X n Y as per your need of shadow to appear to like right bottom or left bottom or so on*/
_viewShadow.layer.shadowOpacity = 0.5f;
_viewShadow.layer.shadowPath = shadowPath.CGPath;
Also masksToBounds is imp as it disables the clipping of sublayers that extend further than the view's bounds. If you put it YES then you won't see shadow as it clips sublayer where else in NO it allow to extent layer.
In Swift 3, CGSizeMake no longer exists. It has been changed to CGSize(width: 20, height: 10). So the shadowOffset can be set like this in Swift 3:
myView.layer.shadowOffset = CGSize(width: 20, height: 10)
I found out that these values give a nice result :
myView.layer.shadowColor = UIColor.black.cgColor
myView.layer.shadowOpacity = 0.25
myView.layer.shadowRadius = 3
myView.layer.shadowOffset = CGSize(width: 1, height: 1) // shadow on the bottom right
I think your shadow offset is incorrect. It should be { 10 , 10} like:
[layer setShadowOffset:CGSizeMake( 10 , 10 ) ];

Resources