- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
最佳答案
好的,让我们开始创建自定义 UIButton
子类
class ButtonWithBadge: UIButton {
}
现在让我们创建代表红点的UIView
let badgeView: UIView = {
let view = UIView()
view.layer.cornerRadius = 3
view.backgroundColor = .red
return view
}()
然后为该子类重写 init
并在内部将此 badgeView
添加到按钮的右上角:设置其约束(右侧和顶部等于按钮的 anchor 和宽度以及badgeView
的 cornerRadius 值的两倍的高度)
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(badgeView)
badgeView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
badgeView.rightAnchor.constraint(equalTo: rightAnchor, constant: 3),
badgeView.topAnchor.constraint(equalTo: topAnchor, constant: 3),
badgeView.heightAnchor.constraint(equalToConstant: badgeView.layer.cornerRadius*2),
badgeView.widthAnchor.constraint(equalToConstant: badgeView.layer.cornerRadius*2)
])
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
接下来创建代表按钮当前状态的变量:
var isRead: Bool = false
现在让我们创建一些方法来根据 isRead
值隐藏或取消隐藏 badgeView
func setBadge() {
badgeView.isHidden = isRead
}
现在我们有了函数,对吧?所以让我们在 init
的末尾和 isRead
变量的 didSet
中调用这个函数
class ButtonWithProperty: UIButton {
var isRead: Bool = false {
didSet {
setBadge()
}
}
override init(frame: CGRect) {
...
setBadge()
}
首先为按钮和 View 创建变量
lazy var barButton: ButtonWithProperty = {
let button = ButtonWithProperty()
... // set color, title, target, etc.
return button
}()
现在例如在 viewDidLoad
中将此 barButton
添加到 UINavigationBar
并将其放置在您想要的位置:
override func viewDidLoad() {
super.viewDidLoad()
...
guard let navigationBar = self.navigationController?.navigationBar else { return }
navigationBar.addSubview(barButton)
barButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
barButton.rightAnchor.constraint(equalTo: navigationBar.rightAnchor, constant: -20),
barButton.bottomAnchor.constraint(equalTo: navigationBar.bottomAnchor, constant: -6)
])
}
现在,当您需要时,您可以轻松更改 barButton
的 isRead
变量,红点消失或出现
barButton.isRead = true
class ButtonWithProperty: UIButton {
var isRead: Bool = false {
didSet {
setBadge()
}
}
lazy var badgeView: UIView = {
let view = UIView()
view.layer.cornerRadius = 3
view.backgroundColor = .red
return view
}()
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(badgeView)
badgeView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
badgeView.rightAnchor.constraint(equalTo: rightAnchor, constant: 3),
badgeView.topAnchor.constraint(equalTo: topAnchor, constant: 3),
badgeView.heightAnchor.constraint(equalToConstant: badgeView.layer.cornerRadius*2),
badgeView.widthAnchor.constraint(equalToConstant: badgeView.layer.cornerRadius*2)
])
setBadge()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setBadge() {
badgeView.isHidden = isRead
}
}
内部 View Controller :
class ViewController: UIViewController {
lazy var barButton: ButtonWithProperty = {
let button = ButtonWithProperty()
... // color, title, target, etc.
return button
}()
...
override func viewDidLoad() {
super.viewDidLoad()
...
guard let navigationBar = self.navigationController?.navigationBar else { return }
navigationBar.addSubview(barButton)
barButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
barButton.rightAnchor.constraint(equalTo: navigationBar.rightAnchor, constant: -20),
barButton.bottomAnchor.constraint(equalTo: navigationBar.bottomAnchor, constant: -6)
])
}
...
}
关于ios - 自定义 BarButtonItem 项目 : Add red dot on top right corner, Swift 4 IOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53846295/
我有一个 Segment 类和一个这样的段数组: private static class Segment { int number, type; Segment(in
我在 SO 中看到一些创建 multilanguage websites in JavaScript 的好建议包括 this article on JavaScript internationaliz
我们有浏览器前缀或黑客 (for Google and Safari) text-align: -webkit-right; (for Firefox) text-align:
过去几天我一直在关注这个问题,我正处于需要寻求帮助的地步。 http://cub.northnodes.com/index.php/about/mission/ 我需要立即捐赠 列一直 float 到
When I press right ctrl, I want the right shift the text will align right. When I press left ctrl le
我已经将右侧的列拆分为顶部和底部。在每个部分中,我在执行以下操作时遇到问题:我希望顶部占据左列高度的 50%,底部占据左列高度的另外 50%。 +-------------------+-------
我知道这个问题的标题很糟糕。对不起。 我有四个 div similar to this .我想要做的只是让 div 编号 2 和 4 之间的垂直空间被删除,而不改变 HTML 的结构。是否可以仅使用
我将表格设置为 100% 宽度。我会添加一个带有 php 的随机 div,有时会充满广告。我希望广告 div 位于表格的右侧和内容。我希望表格位于左侧,但仍为 100% 左右,它将填充广告 div 左
这个问题在这里已经有了答案: Bootstrap align navbar items to the right (24 个答案) 关闭 5 年前。
.floatright { float: right;margin: 0 0 10px 10px;clear: right;width:60px; height:60px; } Lorem
我正在尝试将 td 中的某些内容右对齐。 align="right"有效,但 text-align:right 无效。这是一个 jsfiddle显示这两种情况的示例。除了右对齐右列外,这两种情况是相同
在设计网站时,您认为用于特定任务的最佳图像格式是什么? 在试图找出用于特定任务的格式时,我总是发现自己处于两难境地……例如,我应该全面使用 .jpg 吗?或者,我何时以及为什么应该使用 .png? 例
我是一个 MySQL 新手,今天我尝试设置一个超过 5 行的 MySQL 调用。我不断收到语法错误,我尝试修复了几个小时,但我不知道问题出在哪里。这是代码: USE myDatabase; DELIM
这让我发疯。我有一个 div float 到另一个 div 的右侧,如下所示: Current Membership: 我有以下 css 规则: div#container { f
我有以下代码片段,它会产生不需要的“填充”区域,而填充为零。如何避免这个区域? 代码 div.left { background-color: red; max-width: 25%; f
在 C++ 中,表达式 left() = right() 求值 right() left() 按那个顺序。 right() 先行,正如已讨论过的 here. 我想不出让 right() 先走的理由。你
我有一个很小的菜单列表,当鼠标靠近时它应该会增长。在其原始状态下,菜单是右对齐的,悬停时每第二个元素向右移动并左对齐以为增加的高度腾出空间(参见 JSFiddle )。 ul { font-siz
td.myclass{ width: 6em; text-align: right; padding-right: 2em; } 如您所见,我希望单元格中的文本右对齐,距离单元
你怎么能看到 http://jsfiddle.net/73wst/ 我想在停止下开始,但我不知道如何设置它的样式。 我的 HTML: Stop Start 我的 CSS: .sta
一个大的内部 div 在一个小的外部 div 中,并且外部 div 溢出自动。但是为什么没有内部 div margin-right 和外部 div padding-right? html
我是一名优秀的程序员,十分优秀!