ios - 如何删除 UIButton 和 UIView 上的特定边框?

标签 ios xcode swift uiview uibutton

我有一个 UIButton 和一个 UIView,我想删除按钮的底部边框和 View 的顶部边框。下图显示了一个 UIButton。当我按下这个 UIButton 时,UIView 将被添加为 subview (如下拉菜单),但我希望按钮和 View 彼此合并,这样它看起来就像在“盒子”上。

我知道如何设置边框宽度和颜色:

self.layer.borderWidth = 1
self.layer.borderColor = UIColor(CGColor: "#616366".CGColor).CGColor

但我不知道是否可以在边界线上删除。希望大家能帮忙 - 谢谢

enter image description here

最佳答案

最简单的方法是稍微改变你的布局,这样你就可以将它们作为同级添加,而不是将 UIView 添加为 UIButton 的 subview 到容器 View ,并在容器 View 上绘制边框。

有点像这样:

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

关于ios - 如何删除 UIButton 和 UIView 上的特定边框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33922101/

相关文章:

swift - 检测 iPhone 何时被拔出和插入

ios - 您可以在 iOS 上保存编辑后的 ​​RAW .dng 文件吗?

ios - 在 NSMutableString 上使用 substringFromIndex

ios - 提交到应用商店时 Cordova "Apple will stop accepting submissions of apps that use UIWebView"警告

ios - iPhone X : Incorrect launch-screen orientation used

iphone - 何时使用 Xcode 分布式构建功能

ios - 将用户注册到 firebase 并登录(如果已注册)

ios - 自定义scrollView,当滚动时,移动标题 View

iphone - 后台网络检查

ios - Swift:无法正确访问 JSON 代码。 (打开包装时发现为零)