我有一个简单的 SwiftUI 视图,其中包含 3 个文本元素:
\nstruct ImageDescriptionView: View {\n var title: String?\n var imageDescription: String?\n var copyright: String?\n\n var body: some View {\n VStack(alignment: .leading) {\n \n if let title = title {\n Text(title)\n .fontWeight(.bold)\n .foregroundColor(.white)\n .frame(maxWidth: .infinity, alignment: .leading)\n }\n \n if let imageDescription = imageDescription {\n Text(imageDescription)\n .foregroundColor(.white)\n .fontWeight(.medium)\n .frame(maxWidth: .infinity, alignment: .leading)\n }\n \n if let copyright = copyright {\n Text(copyright)\n .font(.body)\n .foregroundColor(.white)\n .frame(maxWidth: .infinity, alignment: .leading)\n }\n \n }\n .background(\n Color.blue\n )\n }\n}\n
Run Code Online (Sandbox Code Playgroud)\nSwiftUI …
我使用水平滚动和自定义布局计算取决于UICollectionView的安全区域的itemSize.
但对于iPhone X安全区域,不同的方向有不同的大小.我的问题是如何在viewWillTransition函数中计算横向方向的安全区域大小?或者如果没有这个计算怎么办?
使用xcode 9创建一些新的视图控制器,所以现在我有几个安全区域需要处理.
我目前正在尝试做一些全面的事情,这意味着保持不安全的区域(因为我总是显示状态栏)并且背景颜色延伸到全屏(以保持与我以前相似的行为).
另外需要注意的是,这也会影响页面控件,因为当你有一些系统时,系统会把它们放在底部的不安全区域,这个区域也会以黑色显示.
我找不到让背景颜色延伸到不安全区域后面的方法.有什么想法吗?
iOS 11中引入的新安全区域布局指南非常适合防止内容显示在条形下方,但它不包括键盘.这意味着当显示键盘时,内容仍然隐藏在它后面,这是我想要解决的问题.
我的方法是基于听取键盘通知,然后通过调整安全区域additionalSafeAreaInsets
.
这是我的代码:
override func viewDidLoad() {
let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
notificationCenter.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
notificationCenter.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
}
//MARK: - Keyboard
extension MyViewController {
@objc func keyboardWillShow(notification: NSNotification) {
let userInfo = notification.userInfo!
let keyboardHeight = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.height
additionalSafeAreaInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardHeight, right: 0)
UIView.animate(withDuration: 0.3) {
self.view.layoutIfNeeded();
}
}
@objc func keyboardWillHide(notification: NSNotification) {
additionalSafeAreaInsets = …
Run Code Online (Sandbox Code Playgroud) 我刚刚将我的应用程序从支持iOS 8改为支持iOS 9及更高版本.
由于我不使用故事板来创建我的视图,我想知道是否有编程方式的"使用安全区域指南"选项或类似的东西.
我试图锚定我的视图,但它们在iPhone X模拟器中保持顶部和底部重叠.
我目前将我的布局设计设置为一个视图控制器上的全屏滚动视图,其中我将其他视图控制器添加为子视图以创建分页效果.在普通的iPhone屏幕上,它工作得很漂亮.然而,当在iPhone X上运行时,事物似乎偏离中心,我可以在一个页面中多次翻页.
这是我设置scrollview的代码
self.scrollView.contentSize = CGSize(width: self.view.frame.width, height: self.view.frame.size.height * 3)
if #available(iOS 11.0, *) {
self.scrollView.contentInsetAdjustmentBehavior = .never
} else {
// Fallback on earlier versions
}
let V1 = self.storyboard?.instantiateViewController(withIdentifier: "S1") as! UINavigationController!
self.addChildViewController(V1!)
self.scrollView.addSubview(V1!.view)
V1?.didMove(toParentViewController: self)
V1?.view.frame = scrollView.bounds
myViewsArray.append(V1!)
let V2 = self.storyboard?.instantiateViewController(withIdentifier: "S2") as UIViewController!
self.addChildViewController(V2!)
self.scrollView.addSubview(V2!.view)
V2?.didMove(toParentViewController: self)
V2?.view.frame = scrollView.bounds
myViewsArray.append(V2!)
var V1Frame: CGRect = V1!.view.frame
V1Frame.origin.y = 2*self.view.frame.height
V1?.view.frame = V1Frame
var V2Frame: CGRect = V2!.view.frame
V2Frame.origin.y = (self.view.frame.height)
V2?.view.frame = V2Frame …
Run Code Online (Sandbox Code Playgroud) 默认情况下,iPhone X上的状态栏如下所示:
但我想实现这个目标:
我尝试设置preferredStatusBarStyle
为lightContent
但只有在将状态栏后面的背景设置为黑色后才能使用.
为了修复圆角,我最后添加了另一个圆角的子视图.
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .black
let roundedView = UIView(
frame: CGRect(
x: 0,
y: UIApplication.shared.statusBarFrame.height,
width: view.frame.width,
height: view.frame.height
)
)
roundedView.layer.cornerRadius = 10
roundedView.layer.masksToBounds = true
roundedView.backgroundColor = .white
view.addSubview(roundedView)
let label = UILabel(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 60))
label.text = "Black statusbar!"
label.textAlignment = .center
roundedView.addSubview(label)
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
Run Code Online (Sandbox Code Playgroud)
我想知道这是否是最好的方法..必须有更好的方法来实现这一目标.
UPDATE
这是一个可怕的想法,因为: …
当我呈现仅支持横向的SecondViewController时,第一个UIViewController中的安全区域插入会更改为安全区域插图.
FirstViewController:
class ViewController: UIViewController {
@IBAction func showSecondVC(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "SecondViewController")
self.present(controller, animated: true, completion: {
print("completed")
})
}
override func viewSafeAreaInsetsDidChange() {
print(view.safeAreaInsets)
super.viewSafeAreaInsetsDidChange()
print(view.safeAreaInsets)
}
}
Run Code Online (Sandbox Code Playgroud)
第二个ViewController:
class SecondViewController: UIViewController {
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return [.landscape]
}
@IBAction func dismissYourSelff(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
}
Run Code Online (Sandbox Code Playgroud)
控制台输出:
UIEdgeInsets(top: 44.0, left: 0.0, bottom: 34.0, right: 0.0)
UIEdgeInsets(top: 44.0, left: …
Run Code Online (Sandbox Code Playgroud) 我不知道为什么安全区域覆盖了我在 iPhone X 模拟器上的视图,但在 xCode 的视图调试中似乎没问题。是否有任何选项可以隐藏安全区域视图或将其删除?提前致谢!通过视图调试,我看不到任何视图或没有任何东西覆盖我的视图,没关系,真的很奇怪。
我在故事板中添加了这个 myView,它打开了安全区域布局指南。我尝试将 additionalSafeAreaInsets.top additionalSafeAreaInsets.bottom 设置为零,但它不起作用。
这是我如何做约束:
func setupGroupBView() {
self.groupBView = myView.create()
self.view.addSubview(groupBView)
self.groupBView.snp.makeConstraints({ (make) in
make.width.centerX.centerY.equalTo(self.view)
make.height.equalTo(screenHeight)
})
}
Run Code Online (Sandbox Code Playgroud)
我尝试将 myView 的顶部、底部设置为控制器的 view.top view.bottom 为 -44、-34,但仍然无法正常工作。
请帮忙!!!!
ios ×8
swift ×7
ios11 ×4
iphone-x ×3
xcode9 ×3
autolayout ×1
constraints ×1
keyboard ×1
landscape ×1
objective-c ×1
statusbar ×1
swiftui ×1
uikit ×1
xcode ×1
xcode9-beta ×1