- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 .xib
/.storyboard
中使用自定义 UI 元素时,我已经掌握了使用安全区域的窍门。
我现在有一个 UIButton 子类,它在整个应用程序中无处不在。因为它只是一个子类(而不是 .xib
中的自定义类),所以我不确定如何更新它以满足我的需要。
见下图: 这里黄色的是 UIButton。在“普通”iPhone 上,这种黄色位于屏幕底部。我想要实现的是让按钮一直到达安全区域的底部,同时仍处于相同位置(安全区域上方)。
通常我会将按钮约束到superview.bottom
,并在按钮的.xib
中将内容(titleLabel、buttonImage 等)约束到safearea。底部
。
既然这在这里是不可能的,我该怎么做呢?
我尝试在 UIButton 子类中以编程方式添加约束,但没有用。
例子:
if #available(iOS 11.0, *) {
NSLayoutConstraint.activate([
(titleLabel?.topAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor))!,
(titleLabel?.bottomAnchor.constraint(equalTo: self.safeAreaLayoutGuide.bottomAnchor))!
])
} else {
// Fallback on earlier versions
}
提前致谢!
最佳答案
您可以为此使用以下方法,
UIView
。UILabel
添加到上面创建的 view
作为 subView
。UIButton
添加到上面创建的 view
作为 subView
。应用适当的布局约束
以获得理想的 UI。
func addSaveButton() {
let height: CGFloat = 60 + self.view.safeAreaInsets.bottom //Height based on safe area
//Custom View
let customView = UIView(frame: CGRect.init(x: 0, y: self.view.bounds.height - height, width: self.view.bounds.width, height: height))
customView.backgroundColor = #colorLiteral(red: 0.9529411793, green: 0.6862745285, blue: 0.1333333403, alpha: 1)
//Save Label
let label = UILabel()
label.text = "Save"
label.textColor = UIColor.black
//Button
let button = UIButton(frame: customView.bounds)
button.addTarget(self, action: #selector(onTapSaveButton), for: .touchUpInside)
//Add label, button as subview in customView
customView.addSubview(label)
customView.addSubview(button)
self.view.addSubview(customView)
customView.translatesAutoresizingMaskIntoConstraints = false
label.translatesAutoresizingMaskIntoConstraints = false
button.translatesAutoresizingMaskIntoConstraints = false
//Add constraints
NSLayoutConstraint.activate([
self.view.leadingAnchor.constraint(equalTo: customView.leadingAnchor),
customView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
customView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
customView.heightAnchor.constraint(equalToConstant: height),
label.topAnchor.constraint(equalTo: customView.topAnchor, constant: 10),
label.trailingAnchor.constraint(equalTo: customView.trailingAnchor, constant: -10),
button.topAnchor.constraint(equalTo: customView.topAnchor),
button.leadingAnchor.constraint(equalTo: customView.leadingAnchor),
button.trailingAnchor.constraint(equalTo: customView.trailingAnchor),
button.bottomAnchor.constraint(equalTo: customView.bottomAnchor)
])
}
@objc func onTapSaveButton() {
print("Save button pressed")
}
在 iPhone-X 中
在 iPhone-8 中
方法 2:
您可以通过使用按钮的
titleEdgeInsets
来遵循更简单的方法。
func addSaveButton() {
let height: CGFloat = 60 + self.view.safeAreaInsets.bottom
//Button
let button = UIButton(frame: CGRect.init(x: 0, y: UIScreen.main.bounds.height - height, width: UIScreen.main.bounds.width, height: height))
button.setTitle("Save", for: .normal)
button.backgroundColor = #colorLiteral(red: 0.9529411793, green: 0.6862745285, blue: 0.1333333403, alpha: 1)
button.contentHorizontalAlignment = .right
button.contentVerticalAlignment = .top
button.titleEdgeInsets.top = 10
button.titleEdgeInsets.right = 10
button.addTarget(self, action: #selector(onTapSaveButton), for: .touchUpInside)
self.view.addSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false
//Add constraints
NSLayoutConstraint.activate([
self.view.leadingAnchor.constraint(equalTo: button.leadingAnchor),
button.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
button.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
button.heightAnchor.constraint(equalToConstant: height)
])
}
您可以在 storyboard/subclassing
中轻松地做同样的事情。我认为这个比上一个好。
方法 3:
子类 UIButton
并使用它以编程方式创建按钮。
class CustomButton: UIButton {
override func draw(_ rect: CGRect) {
self.contentHorizontalAlignment = .right
self.contentVerticalAlignment = .top
self.titleEdgeInsets.top = 10
self.titleEdgeInsets.right = 10
}
}
关于ios - UIButton 子类,填充 SafeArea,插入内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56182815/
我在尝试以编程方式布局 View 时遇到问题,我似乎无法找到一种简洁、非 hacky 的方法来修复它。 我正在使用 safeAreaInsets 来调整我 View 中某些元素的大小。在我在 iOS
我正在设计一个视频通话屏幕,我必须在其中管理孔屏幕之间的 selfViewContainer。 在这个屏幕上,我已经管理了水平对齐的大部分部分,但对于垂直,它在有缺口的设备上无法正常工作。 我想在拖动
嘿,我在下面的屏幕上使用 SafeArea 和 BackButton。这里的后退按钮不适用于 SafeArea。 有谁知道如何解决这个问题? class _ProfileScreenState ext
我正在尝试为带有彩色系统栏的 flutter 应用程序添加 SafeArea 小部件,但不知何故它们总是变黑。 @override Widget build(BuildContext cont
我正在尝试获取小部件的高度,但它打印的值相同 I/flutter (19253): full height: 976.0 I/flutter (19253): safe height: 976.0 我
当前行为 嗨,大家好, 我想为底部选项卡设置背景颜色。所以我做了如下。 问题是 SafeArea 有白色背景 预期行为 我期望的是 那么你能告诉我如何在 Rea
有没有办法可以检查运行该应用程序的设备是否需要 SafeArea。我需要根据这个 true 或 false 值运行代码。我不需要使用 SafeArea 小部件。如果设备需要填充,我只需要正确或错误的答
我正在尝试调整我的游戏中的 UI 以正确适应 iPhone X 的屏幕。 在我的代码中,我试图编写代码行 return Screen.safeArea 但是,“safeArea”以红色突出显示,当我将
我正在尝试设置一个水平滚动的 Collection View ,其中的单元格大小为 CGSize(width: view.frame.width, height: view.frame.height)
我正在尝试为 iPhone X 更新我的应用程序,但没有“安全区域”指南。它们只在 Storyboards 中,而不是单独的 nibs 中吗? 最佳答案 安全区域布局指南也可用于 Nib 。当您在 X
当我向 Widget 提供 SafeArea 时,它会从凹口和主页按钮(iPhone X + 中的水平线)获得一些余量。如何更改不安全区域的背景? (边缘部分)? 最佳答案 将您的 SafeArea
我刚刚添加了一个 ListView 作为 Scaffold > Stack 的子项,它的顶部似乎有 SafeArea。列不存在这个问题。有什么办法可以去掉吗? Container( color:
我正在为屏幕使用 MaterialApp 和 Scaffold + SafeArea。在我尝试使用持久的 BottomSheet 之前,一切都很好。 BottomSheet 内容忽略 SafeArea
我正在尝试理解 SafeArea Flutter 中的小部件。 SafeArea 代码添加到 Flutter Gallery 应用程序 here在 github 中到处显示 top:false 和 b
有没有办法检测和避免手机屏幕上的缺口(例如,iPhone X 中的摄像头缺口),但忽略屏幕的圆角? 例如,这是我在 iPhone 上的横向模式下的应用程序,带有一个缺口: 左边的菜单很好——屏幕的圆角
我很好奇我是否应该始终在屏幕的顶层使用 SafeArea 小部件。我的意思是......我什么时候希望我的内容被诸如缺口之类的东西阻止?对我来说,答案似乎永远不会。 那么我不应该只在每个页面上使用 S
我正在尝试在带缺口的 Android 设备上占用尽可能多的可用空间。在 Flutter 开发中,有一个名为 SafeArea 的小部件,它会根据设备是否有凹槽自动调整您的 View 。即使在 iOS
在 .xib/.storyboard 中使用自定义 UI 元素时,我已经掌握了使用安全区域的窍门。 我现在有一个 UIButton 子类,它在整个应用程序中无处不在。因为它只是一个子类(而不是 .xi
我想知道有什么办法可以为 iPhone X SafeArea 添加两种不同的颜色吗? 在 React Native 上,可以通过添加两个 SafeAreaView 来修复此问题。有谁知道如何解决 fl
如您所见,我想将搜索栏恰好放在 safeArea 的顶部,但是 proxy.safeAreaInsets 没有正确的值,因为在PreviewProvider 父级使用 edgesIgnoringSaf
我是一名优秀的程序员,十分优秀!