【问题标题】:How to remove specific border on an UIButton and an UIView?如何删除 UIButton 和 UIView 上的特定边框?
【发布时间】:2016-02-28 13:45:23
【问题描述】:
我有一个 UIButton 和一个 UIView,我想在其中删除按钮的底部边框和视图的顶部边框。下图显示了一个 UIButton。当我按下此 UIButton 时,UIView 将被添加为子视图(如下拉菜单),但我希望按钮和视图相互合并,因此它看起来像“框”。
我知道如何设置边框宽度和颜色:
self.layer.borderWidth = 1
self.layer.borderColor = UIColor(CGColor: "#616366".CGColor).CGColor
但我不知道是否可以在边界线上删除。希望你们能帮忙-谢谢
【问题讨论】:
标签:
ios
xcode
swift
uiview
uibutton
【解决方案1】:
最简单的方法是稍微改变你的布局,而不是将UIView 添加为UIButton 的子视图,而是将它们作为兄弟姐妹添加到容器视图中,并在其上绘制边框容器视图。
有点像这样:
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 44))
button.setTitle("Button", forState: .Normal)
button.titleLabel?.textColor = UIColor.blackColor()
let container = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 44))
container.addSubview(button)
container.frame = button.frame
container.backgroundColor = UIColor.whiteColor()
container.layer.borderWidth = 5
container.layer.borderColor = UIColor.blackColor().CGColor
let added = UIView(frame:CGRect(x: 0, y: 44, width: 100, height: 200))
added.backgroundColor = UIColor.blueColor()
container.addSubview(added)
container.frame.size.height += added.frame.size.height
XCPlaygroundPage.currentPage.liveView = container