简体   繁体   English

左右两侧的UIButton边框

[英]UIButton border on right and left sides

I've been having trouble getting a border or shadow between three buttons to show some separation. 我一直无法在三个按钮之间显示边框或阴影以显示一些分离。 I have tried getting a border or shadow on just the left and right side of the middle button but I could only get the shadow on one side. 我已经尝试在中间按钮的左侧和右侧获得边框或阴影,但是我只能在一侧获得阴影。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

What I have tried to user to get a shadow but only shows on right side: 我曾尝试向用户显示阴影,但仅在右侧显示:

    middleButton.layer.backgroundColor = UIColor.whiteColor().CGColor
    middleButton.layer.borderColor =  UIColor(red: 208/255, green: 208/255, blue: 208/255, alpha: 1.0).CGColor
    middleButton.layer.borderWidth = 0.0
    middleButton.layer.masksToBounds = false
    middleButton.layer.shadowColor = UIColor(red: 208/255, green: 208/255, blue: 208/255, alpha: 1.0).CGColor
    middleButton.layer.shadowOffset = CGSizeMake(0.5, 1.0)
    middleButton.layer.shadowOpacity = 1.0
    middleButton.layer.shadowRadius = 1.0

Below is my current view with the three buttons that I am trying to add a separator between: 下面是我当前的视图,其中有三个按钮,我试图在它们之间添加分隔符:

在此处输入图片说明

The hierarchy I have for it in a table cell: 我在表格单元格中拥有的层次结构:

在此处输入图片说明

Swift 3 Answer 斯威夫特3答案

You can make an extension for UIButton 您可以扩展UIButton

extension UIButton {

 func addRightBorder(borderColor: UIColor, borderWidth: CGFloat) {
    let border = CALayer()
    border.backgroundColor = borderColor.cgColor
    border.frame = CGRect(x: self.frame.size.width - borderWidth,y: 0, width:borderWidth, height:self.frame.size.height)
    self.layer.addSublayer(border)
 }

 func addLeftBorder(color: UIColor, width: CGFloat) {
    let border = CALayer()
    border.backgroundColor = color.cgColor
    border.frame = CGRect(x:0, y:0, width:width, height:self.frame.size.height)
    self.layer.addSublayer(border)
 }
}

Then you can use this for any of your buttons. 然后,您可以将其用于任何按钮。

middleButton.addRightBorder(borderColor: UIColor.white, borderWidth: 1.0)

middleButton.addLeftBorder(borderColor: UIColor.white, borderWidth: 1.0)

Should work fine. 应该工作正常。 Happy Coding ! 编码愉快!

Where there are limits on the actual code possibilities, the solution lay with the graphic icons you have for 'Favourite', 'Share', Map'. 在实际代码可能性有限的情况下,解决方案在于为“收藏夹”,“共享”,“地图”提供图形图标。

A separator border / line between the buttons can be done in the graphic icon creation for the buttons you have. 按钮之间的分隔线边框/线条可以在创建具有按钮的图形图标时完成。 Adobe illustrator or photoshop . Adobe illustratorphotoshop I have recreated a sample in illustrator, brought it over to 'Assets.xcassets' as an image and added it as background image to the button. 我已经在illustrator中重新创建了一个样本,将其作为图像移到“ Assets.xcassets”中, 并将其作为背景图像添加到了按钮中。

Button eg 按钮,例如

在此处输入图片说明

Snapshop of simulator as follows... 模拟器的Snapshop如下...

在此处输入图片说明

Another approach is to add your buttons to UIStackView. 另一种方法是将按钮添加到UIStackView。 With it you can easily manage how you want the subviews to be arranged and distributed. 有了它,您可以轻松地管理希望子视图的排列和分布方式。

Add UIView's as separators with a width constraint of 1 point. 将UIView添加为宽度限制为1点的分隔符。 After setting your constraints on the UIStackView, set distribution property to "Equal Spacing" and the views will align according to it. 在UIStackView上设置约束后,将distribution属性设置为“ Equal Spacing”,视图将根据其对齐。

例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM