www.91084.com

GVKun编程网logo

在 iOS8 中使用 Swift 更改特定 ViewController 的状态栏颜色(swiftui 切换view)

40

对于在iOS8中使用Swift更改特定ViewController的状态栏颜色感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍swiftui切换view,并为您提供关于iOSSwiftwindo

对于在 iOS8 中使用 Swift 更改特定 ViewController 的状态栏颜色感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍swiftui 切换view,并为您提供关于iOS Swift window.rootViewController vs presentViewController、ios – Swift 3 – 覆盖UINavigationController的初始化程序以设置rootviewcontroller、ios – Swift playground:获取ViewController以调用presentViewController、ios – Swift – 如何从ViewController中按下UItableViewCell中的动作按钮?的有用信息。

本文目录一览:

在 iOS8 中使用 Swift 更改特定 ViewController 的状态栏颜色(swiftui 切换view)

在 iOS8 中使用 Swift 更改特定 ViewController 的状态栏颜色(swiftui 切换view)

override func preferredStatusBarStyle() -> UIStatusBarStyle { return UIStatusBarStyle.LightContent;}

在任何 ViewController 中使用上述代码将特定视图控制器的状态栏颜色设置为白色 对我来说在 iOS8 中不起作用
。有什么建议么?使用 UIApplication.sharedApplication 方法,在整个应用程序的 Info.plist
中进行所需更改后,颜色会发生变化。

// Change the colour of status bar from black to whiteUIApplication.sharedApplication().statusBarStyle = .LightContent

如何更改某些必需和 特定 ViewControllers 的状态栏颜色?

答案1

小编典典

在阅读了所有建议并尝试了一些事情之后,我可以使用以下步骤使其适用于特定的视图控制器:

第一步:

打开你的 info.plist 并插入一个名为“ 查看基于控制器的状态栏外观 ”的新键到 NO

第二步(只是解释,不需要实现):

通常我们在 AppDelegate 的 application(_:didFinishLaunchingWithOptions:) 方法中放入如下代码,

斯威夫特 2

UIApplication.sharedApplication().statusBarStyle = .LightContent

斯威夫特 3

UIApplication.shared.statusBarStyle = .lightContent

但这 会影响statusBarStyle所有 ViewControllers。

那么,如何使它适用于特定的 ViewController - 最后一步:

打开要更改的视图控制器文件,statusBarStyle然后将以下代码放入viewWillAppear()

斯威夫特 2

UIApplication.sharedApplication().statusBarStyle = .LightContent

斯威夫特 3

UIApplication.shared.statusBarStyle = .lightContent

此外,实现该viewWillDisappear()特定 viewController 的方法并放入以下代码行,

斯威夫特 2

override func viewWillDisappear(animated: Bool) {    super.viewWillDisappear(animated)    UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default}

斯威夫特 3

override func viewWillDisappear(_ animated: Bool) {    super.viewWillDisappear(animated)    UIApplication.shared.statusBarStyle = UIStatusBarStyle.default}

此步骤将首先更改statusBarStyle特定视图控制器的
,然后将其更改回default特定视图控制器消失时。不执行viewWillDisappear()statusBarStyle永久更改为新定义的值UIStatusBarStyle.LightContent

iOS Swift window.rootViewController vs presentViewController

iOS Swift window.rootViewController vs presentViewController

在多个视图之间切换的最佳做法是什么?更改rootViewController或使用模态视图?

设置rootviewController:

let storyBoard : UIStoryboard = UIStoryboard(name: "Main",bundle: nil)
var vc : UIViewController = storyBoard.instantiateViewControllerWithIdentifier("viewTarget") as TargetViewController
var window :UIWindow = UIApplication.sharedApplication().keyWindow!
window.rootViewController = vc;
window.makeKeyAndVisible()

更改模态视图:

let storyBoard : UIStoryboard = UIStoryboard(name: "Main",bundle: nil)
let initViewController: UIViewController = storyBoard.instantiateViewControllerWithIdentifier("viewTarget") as TargetViewController
self.presentViewController(initViewController,animated: false,nil)

当我需要向用户呈现其他视图时,我很困惑.

附:就我而言,我的应用程序以登录表单作为rootViewController开始.登录后,我认为最好更改rootViewController,但我是对的吗?

解决方法

我的建议是,而不是困扰很多,你只需要覆盖rootviewController剩下的东西由你的应用程序照顾.

let storyBoard : UIStoryboard = UIStoryboard(name: "Main",bundle: nil)
    let initViewController: UIViewController = storyBoard.instantiateViewControllerWithIdentifier("viewTarget") as TargetViewController
    self.window?.rootViewController? = initViewController

在这段代码中,我们只是从AppDelegate覆盖了rootview控制器.

ios – Swift 3 – 覆盖UINavigationController的初始化程序以设置rootviewcontroller

ios – Swift 3 – 覆盖UINavigationController的初始化程序以设置rootviewcontroller

我正在使用 Swift 3来覆盖使用rootviewcontroller初始化我的导航控制器的init方法(并将rootviewcontroller委托设置为self).但是,我收到以下错误:

Incorrect argument label in call (have ‘rootViewController:’,expected
‘coder:’)

class NavigationController: UINavigationController,RootViewControllerDelegate {

    let rvc = RootViewController()

    convenience init() {
        self.init(rootViewController: rvc) // Incorrect argument label in call (have 'rootViewController:',expected 'coder:')
        self.rvc.delegate = self
    }

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

有人可以解释一下我做错了吗?我最初尝试使用

override func init()

但Xcode最终让我将其转换为

convenience init()

解决方法

init(rootViewController :)在UINavigationController中定义,它是NavigationController类的超类.因此,您应该使用super而不是self来引用它:

convenience init() {
    super.init(rootViewController: rvc)
    self.rvc.delegate = self
}

由于您在NavigationController中定义了另一个初始化程序,Xcode认为您正在尝试调用该初始化程序.这就是为什么它告诉你把编码器作为参数标签.

ios – Swift playground:获取ViewController以调用presentViewController

ios – Swift playground:获取ViewController以调用presentViewController

我想在Playground玩UIAlertController.是否有可能从XCPlaygroundPage获取ViewController – 能够调用presentViewController?

解决方法

是的,你当然可以获得一个视图控制器,但因为没有“附加”视图控制器,你最终会看到如下警告:

不鼓励在分离的视图控制器上呈现视图控制器

在我的情况下,警报控制器在屏幕上超短暂闪烁,然后消失.也许你可以稍微提高一点吗?

无论如何,这是我在操场上创建视图控制器的方式:

import UIKit
import XCPlayground

class RootViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.frame = CGRect(x: 0,y: 0,width: 350,height: 420)
        self.view.backgroundColor = UIColor.redColor()
    }
}

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    var tableView: UITableView!
    let items = ["Hello 1","Hello 2","Hello 3"]
    var alertController : UIAlertController? {
        didSet {
            if alertController == nil {
                print("alertController set to nil")
            } else {
                print("alertController set to \(alertController)")
            }
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.frame = CGRect(x: 0,width: 320,height: 480)
        self.tableView = UITableView(frame:self.view.frame)
        self.tableView!.dataSource = self
        self.tableView!.registerClass(UITableViewCell.self,forCellReuseIdentifier: "cell")
        self.view.addSubview(self.tableView)

    }
    func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int{
        return self.items.count;
    }

    func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        let cell = tableView.dequeueReusableCellWithIdentifier("cell",forIndexPath: indexPath) as UITableViewCell
        cell.textLabel?.text = "\(self.items[indexPath.row])"
        return cell
    }

    override func viewDidAppear(animated: Bool) {
        alertController = UIAlertController.init(title: "My Title",message: "Testing in Playground",preferredStyle: .Alert)

        let okAction = UIAlertAction(title: "Ok",style: .Default) { (_) -> Void in
            // Some action here
            print("hit okay button")
        }

        alertController!.addAction(okAction)

        ctrl.presentViewController(alertController!,animated: false,completion: {
            print("done presenting")
        })
    }
}

var ctrl = ViewController()
var rootVC = RootViewController()
rootVC.addChildViewController(ctrl)
rootVC.view.addSubview(ctrl.view)
XCPlaygroundPage.currentPage.liveView = rootVC.view

而我在这里做的是创建一个“根视图控制器”,添加一个带有表的子视图控制器(试图让一些视图控制器“附加”到其他东西)然后我试图呈现一个警报控制器在那.

ios – Swift – 如何从ViewController中按下UItableViewCell中的动作按钮?

ios – Swift – 如何从ViewController中按下UItableViewCell中的动作按钮?

参见英文答案 > Issue Detecting Button cellForRowAt                                    3个
>             swift: how to get the indexpath.row when a button in a cell is tapped?                                    14个
我在UITableViewCell中有一个操作按钮,我想检测按钮的按下时间以及ViewController中按下的单元格编号,以便在ViewController.swift中创建音频播放列表.

我已经陷入这个问题一段时间了,我真的很赞赏你的建议.这是代码.

ViewController.swift

import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self

        tableView.register(UINib(nibName: "Cell",bundle: nil),forCellReuseIdentifier: "cell")

    }

    func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        return 3
    }

    func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! Cell
        return cell

    }


}

Cell.swift

import UIKit

class Cell: UITableViewCell {

    @IBOutlet weak var button: UIButton!

    @IBAction func buttonpressed(_ sender: Any) {

        ***[Code to send the pressed cell's number to ViewController]***

    }

}

解决方法

你可以选择一个老式的代表模式.这样做的好处是不会将视图控制器与单元格耦合.不要忘记让你的代表弱,以避免保留周期.

您可以从表视图中找到单元索引路径. (我假设按单元格编号表示索引路径)

protocol CellDelegate: class {
    func didTap(_ cell: Cell)
}

class Cell: UITableViewCell {

    weak var delegate: CellDelegate?
    @IBAction func buttonpressed(_ sender: Any) {
        delegate?.didTap(self)
    }
}

class ViewController: UIViewController,CellDelegate {

    func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = ...
        cell.delegate = self
        return cell
    }

    func didTap(_ cell: Cell) {
        let indexPath = self.tableView.indexPath(for: cell)
        // do something with the index path
    }
}

今天关于在 iOS8 中使用 Swift 更改特定 ViewController 的状态栏颜色swiftui 切换view的讲解已经结束,谢谢您的阅读,如果想了解更多关于iOS Swift window.rootViewController vs presentViewController、ios – Swift 3 – 覆盖UINavigationController的初始化程序以设置rootviewcontroller、ios – Swift playground:获取ViewController以调用presentViewController、ios – Swift – 如何从ViewController中按下UItableViewCell中的动作按钮?的相关知识,请在本站搜索。

本文标签: