简体   繁体   English

视图固定在底部 Swift

[英]View fixed at the bottom Swift

I'm sorry if someone had asked that question before, I didn't find the same.对不起,如果之前有人问过这个问题,我没有找到相同的。 I have a view with an image and it has to be always fixed a the bottom in my iOS Swift app.我有一个带有图像的视图,它必须始终固定在我的 iOS Swift 应用程序的底部。 Even if I go to another screen it still has to be at the bottom, and it has to be the same view, not different ones for different screens.即使我转到另一个屏幕,它仍然必须在底部,并且必须是相同的视图,而不是不同屏幕的不同视图。 Thank you谢谢

Make the root view controller for your app a generic UIViewController.使您的应用程序的根视图控制器成为通用 UIViewController。

Add your image view to the bottom of your root view controller将图像视图添加到根视图控制器的底部

Drag a container view onto that view controller.将容器视图拖到该视图控制器上。 Make the container view extend down to just above your image view.使容器视图向下延伸到图像视图的正上方。

Now create a new view controller scene in your storyboard.现在在故事板中创建一个新的视图控制器场景。 It can be a navigation view controller if you want a navigation stack, or a tab bar controller, or whatever type you want.如果你想要一个导航堆栈,或者一个标签栏控制器,或者你想要的任何类型,它可以是一个导航视图控制器。

Control-drag from the container view on your root view controller onto your new view controller.从根视图控制器上的容器视图按住 Control 拖动到新的视图控制器上。 Select "embed" in the dialog that appears.在出现的对话框中选择“嵌入”。 You have now created an embed segue, and your root view controller contains a child view controller.您现在已经创建了一个嵌入转场,并且您的根视图控制器包含一个子视图控制器。

Now, do all your navigation from your child view controller.现在,从您的子视图控制器进行所有导航。 It will always occupy most, but not all of the screen, and the root view controller will still be visible with the image view at its bottom.它总是占据屏幕的大部分,但不是全部,并且根视图控制器仍然可见,图像视图位于其底部。

You can add a custom UIWindow to the bottom of the screen:您可以将自定义 UIWindow 添加到屏幕底部:

Instantiate a UIWindow with a frame (origin and size), and set its rootViewController to a UIViewController containing your UIImageView.使用框架(原点和大小)实例化 UIWindow,并将其rootViewController设置为包含 UIImageView 的 UIViewController。 To show the window, set isHidden to false .要显示窗口,请将isHidden设置为false If you don't want the main window to overlap with the custom window, decrease the main window's height by the height of the custom window.如果您不希望主窗口与自定义窗口重叠,则将主窗口的高度降低自定义窗口的高度。

You can do all of this in application(_:didFinishLaunchingWithOptions:) in your AppDelegate.您可以在application(_:didFinishLaunchingWithOptions:)中完成所有这些操作。

Here is a quick example that I used with an ad banner:这是我与广告横幅一起使用的快速示例:

window?.frame.size.height -= adBannerHeight
adWindow = UIWindow(frame: CGRect(x: 0, y: UIScreen.main.bounds.height - adBannerHeight, width: UIScreen.main.bounds.width, height: adBannerHeight))
adWindow?.rootViewController = AdViewController()
adWindow?.isHidden = false

You can create custom view with image to load every screen as fixed view.您可以使用图像创建自定义视图以将每个屏幕加载为固定视图。

//CusomeView

import UIKit

class CustomeView: UIView {

var contentView: UIView!
override init(frame: CGRect) {
    super.init(frame: frame)

}
required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

}


}

Now you can add this custom View to every viewController where you wants to show in bottom area check below code to load the custom view.现在,您可以将此自定义视图添加到要在底部区域显示的每个视图控制器,请检查下面的代码以加载自定义视图。

You can also create common method for loading custom view.您还可以创建用于加载自定义视图的通用方法。

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var cv: CustomeView!
override func viewDidLoad() {
    super.viewDidLoad()
    if let customView = Bundle.main.loadNibNamed("CustomeView", owner: self, options: nil)?.first as? CustomeView {
        self.view.addSubview(customView)
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

Hope this help希望这有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM