gpt4 book ai didi

ios - 你如何访问 UIViewControllerRepresentable NavigationBar 属性?

转载 作者:行者123 更新时间:2023-12-04 14:26:52 25 4
gpt4 key购买 nike

在 SwiftUI 中,如果您使用 NavigationLink() 转换为 UIViewControllerRepresentable,您会如何;比如说,在导航栏上添加按钮或更改标题属性。

这就是我现在正在做的事情:

import SwiftUI

/// Controls the actual action performed by the button upon taps.
struct CatagoryButton: View {

@State var isPresenting :Bool = false

var company : Company?
var text : String

var body: some View {

NavigationLink(destination: UIKitWrapper(company: self.company, storyboardPointer: self.text)
.navigationBarTitle(self.text)
.edgesIgnoringSafeArea(.all),
isActive: self.$isPresenting,

label: {
Button(action: {
self.isPresenting.toggle()

}){

ZStack {

ButtonShadowLayer(text: text)

GradientBackground()
.mask(ButtonBaseLayer())

CircleAndTextLayer(text: text)
}
}
})
}
}

这是我的可表示的结构。
import SwiftUI

/// Wraps UIKIT instance in a representable that swiftUI can present.
struct UIKitWrapper: UIViewControllerRepresentable {

//Specify what type of controller is being wrapped in an associated type.
typealias UIViewControllerType = UIViewController

//Company property passed from parent view. Represents the company the user selected from main view.
private var company : Company

//Determines which viewcontroller will be presented to user. This string corresponds to the name of the storyboard file in the main bundle.
private var storyboardPointer : String

init(company: Company?, storyboardPointer: String) {

guard let company = company else {fatalError()}

self.company = company
self.storyboardPointer = storyboardPointer
}

func makeUIViewController(context: Context) -> UIViewControllerType {

//Find user defined storyboard in bundle using name.
let storyboard = UIStoryboard(name: storyboardPointer, bundle: .main)

//Downcast returned controller to protocol AccessControllerProtocol. This step is required because we are not sure which storyboard will be accessed. Potential storyboard controllers that can be called all conform to this protocol.
//FIXME: Remove fatalError and create error enum asap.
guard let viewController = storyboard.instantiateInitialViewController() as? AccessControllerProtocol else { fatalError() }

//Assign user selected company object to instance property on incoming viewController.
viewController.company = company

//Return UINavigationController with storyboard instance view controller as root controller.
return viewController
}

func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {

}
}

最后,这是使用可表示的类之一。
import UIKit

class OrdersViewController: UIViewController, AccessControllerProtocol {

var company : Company!
@IBOutlet var companyNameLabel : UILabel!

override func viewDidLoad() {
super.viewDidLoad()
setBackgroundColor()
companyNameLabel.text = company.name
self.navigationController?.navigationItem.rightBarButtonItems = [UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(self.tapRightBarButton))]
}

func setBackgroundColor(){

let backgroundGradient = BackgroundGradientSetter()
let viewWithGradient = backgroundGradient.setGradientToView(with: [DarkBlueHue_DEFAULT,LightBlueHue_DEFAULT], size: view.bounds)

view.addSubview(viewWithGradient)
view.sendSubviewToBack(viewWithGradient)
}

@objc func tapRightBarButton(){

}
}

不管我做什么,这个按钮都不显示。我不确定是否需要将它放在 makeCoordinator() 中,或者我是否缺少某些东西。如果有人有见解,请告诉我!

最佳答案

如果它在 viewDidLoad 中不可用,请尝试拨打您的 setupNavigation()viewWillAppear()

关于ios - 你如何访问 UIViewControllerRepresentable NavigationBar 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60658388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com