Post

Replies

Boosts

Views

Activity

I am using uikit code in SwiftUI.This approach is not metioned anywhere in apple documentation.Is this right approach?
For my use case i need to access the same intance of MyViewController ,which i am using in UIViewRepresentable for adding it to SwiftUI herierchy .And to achive that i am creating a static intance of MyViewController and using it in objective c function to make changes in viewcontroller as well as adding it in SwiftUI view hireiachy . Is this a right apprach to use ? I dont find any documentation by apple which suggest this approach . Just want to validate it here . And also Is this method of using UIRepresentableController right method to use in my UIkit's view controller in SwiftUI and updating viewcontroller from objectiveC ? or is there any other approach to achieve this ? Entry Point : import SwiftUI @main struct MyApp: App { var body: some Scene { WindowGroup { SwiftUIView() } } } SwiftUIView: import SwiftUI struct SwiftUIView: View { var body: some View { UIKitToSwiftUIBridge () } } UIKitToSwiftUIBridge: import SwiftUI import UIKit struct UIKitToSwiftUIBridge: UIViewControllerRepresentable { func makeUIViewController(context: Context) -> MyViewController { let viewcontroller = SharedViewController.sViewController return viewcontroller } func updateUIViewController(_ uiViewController: MyViewController, context: Context) { } } SharedViewController : { static let sViewController = MyViewController () private override init () { } @objc static func GetViewController ()->MyViewController { return SharedViewController.sViewController } } MyViewController.swift : import UIKit class MyViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = .black let button = UIButton (frame : CGRect (x:50, y:100, width:100, height:50)) button.setTitle("Button", for: .normal) button.addTarget(self, action: #selector(ButtonAction (_:)), for: .touchDown) } @objc func ButtonAction (_ sender :UIButton) { let view = UIView (frame: CGRect (x:0, y:0, width:300, height:400)) view.backgroundColor = .red let label = UILabel (frame: CGRect(x: 60, y: 50, width: 150, height: 60)) label.text = " Label" view.addSubview(label) self.view.addSubview(view) } @objc func ChangeColor () { self.view.backgroundColor = .white } } Objective C code where i need to access MyViewController's intance : void ObjectiveC::ChangeColorOfViewController () { dispatch_async( dispatch_get_main_queue (),^{ MyViewController * sharedobj = [SharedObject sharedMethod]; [sharedobj ChangeColor]; }); }
0
4
421
Sep ’23
SwiftUI button added inside uikit view using hosting controller is not triggering a action
When i am using a swiftUI's button with the help of a UIHostingController inside a UIKit based view , Swift UI view with button : - @State var uTitle:String! @State var uFrame:CGRect! var body: some View { Button(action: { self.ButtonAction(pId: uId) }) { Text(uTitle) } } func ButtonAction (pId:Int) { print ("Hello world !!!!!") } } If i use above SwiftUIButton this way ( Adding button.frame.size ) :- let button:UIView! button = UIHostingController(rootView: SwiftUIButton (uTitle: "Button title")).view button.frame.origin = CGPoint(x: 50, y: 50) button.frame.size = CGSize(width: 150, height: 50) parentview.addSubview(button) Button click action works fine. But if i use button this way (not adding width height after intantiation of UIHostingController ,instead adding width height inside SwiftUIButton using .frame(widht: ,height:)) let button:UIView! button = UIHostingController(rootView: SwiftUIButton (uTitle: "Button title")).view button.frame.origin = CGPoint(x: 50, y: 50) // Removed frame.size parentview.addSubview(button) @State var uTitle:String! @State var uFrame:CGRect! var body: some View { Button(action: { self.ButtonAction(pId: uId) }) { Text(uTitle) // Added .frame property .frame(width:150,height:50) } } func ButtonAction (pId:Int) { print ("Hello world !!!!!") } } Action is not triggering. What could be the issue?
1
0
1.2k
Aug ’23
In swiftUI TextField ,can we have some property similar to textfield.text in case of UIkit's TextField so that we do not have to explicitly mantain a State var to hold textfield value?
I am newly shifting to SwiftUI from UIkit , and in UIkit if we have to use a TextField then we could use a delegate method shouldChangeCharacterIn(textfield:UITextField) and access text from textffield.text, for accessing text value of textfield .But in SwiftUI's TextField we have to pass a State variable for accessing value . And in my app , i will have a lot of TextField and i dont want to mantain this much state variables . Is there any way i can get UIkit like behavior in SwiftUI's Textfield ? And also in case of button action ,we can use a func (sender:UIButton) in case of UIkit but in case of SwiftUI we did not get a object of sender ,Is there any way i can achive similar thing in SwiftUI func ?
1
1
520
Jun ’23
How to access a char* value store in cpp file and set Text ,using this value, in SwiftUI Text when a button is clicked?
I am trying to set Text value in my SwiftUI code from a char* string stored in cpp whenever a button is pressed .I was able to do similar think in UIkit based application and since i am new to SwiftUI not able to do same in SwiftUI. SwiftUI code : @state var title = "Title" var body: some View { Text(title) .bold() .font(.system(size: 40)) .frame(width: 250,height: 200,alignment: .center ) Button("Change title", action: ChangeTitle) } func ChangeTitle () { //change value of title } } Want to change the value of var title to char* str in cpp when button is pressed.
1
0
481
May ’23
Can we set a NSString from objC in UITextView to store value from textview.text
Is there any way we can set a NSString from objC in UITextView by extending or without extending UITextView(without extending preferably). Reason -: My logic for action on text change will be triggered from cpp code .That is why i need to send the textview.text to objC on change of character .Is it possible to have a buffer of such type ? ,and if yes, is the memory delocation and allocation of buffer managed by xcode ? If any of the above thing is not possible ,then is there anyother way to achive similar behavior ? Any answer can help.
0
0
473
May ’23
how tabs in safari are implemented in iOS (one we see in landscape mode in iPhone)?
I am trying to implement safari like tabs in ios for my application , i am not able to find any source how apple has implemented that for safari . I tried implementing it by subclassing UITabBarController ,UITabBar and UITabBarItem , in this implementation i am able to put cross buttom in tabs but still not able to achieve same behavior as tabs in safari for ios . For example , in my approach i am not able to find a way to handle the "Tab deck" when number of tabs increases from certain limit .Is there any approach i can follow or artifect i can use to achive such behavior ? or the approach i am using ,is it same approach used by apple for safari tabs in ios? Approach i follow as of now - : import UIKit class TWCustomisedTabBarController: UITabBarController { var twtabbar: TWTabBar! override func viewDidLoad() { super.viewDidLoad() tabBar.isHidden = true setupView() } func setupView() {} func setTabBar(items: [TWTabBarItem]) { guard items.count > 0 else { return } twtabbar = TWTabBar(items: items) for i in 0 ..< items.count { items[i].tag = i items[i].crossbutton.tag = i } guard let bar = twtabbar else { return } self.view.addSubview(bar) } } class TWTabBar: UITabBar { var twitems:[TWTabBarItem] = [] init (items: [TWTabBarItem]) { super.init(frame: CGRect(x: 0, y: 0, width: 320, height: 49)) self.twitems = items setupView() } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } func setupView() { for i in 0 ..< twitems.count { let item = twitems[i] addSubview(item) } } } class TWTabBarItem: UIView { let textLabel = UILabel() let crossbutton = UIButton() let leadingline = UILabel() override init(frame: CGRect) { super.init(frame: frame) setupView() } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } private func setupView() { textLabel.text = "Tab" textLabel.font = UIFont.systemFont(ofSize: 14) textLabel.frame = CGRect(x: 5, y: 0, width: self.frame.width/2, height: self.frame.height) textLabel.textColor = .black crossbutton.setTitle("X", for: .normal) crossbutton.frame = CGRect(x: self.frame.width*3/4, y: 15, width: self.frame.width/4, height: 20) crossbutton.setTitleColor(.black, for: .normal) crossbutton.backgroundColor = .gray crossbutton.titleLabel?.font = UIFont.systemFont(ofSize: 10) leadingline.text = "|" leadingline.font = UIFont.systemFont(ofSize: 25) leadingline.frame = CGRect(x: 0, y: 0, width: 5, height: 49) leadingline.textColor = .black addSubview(leadingline) addSubview(crossbutton) addSubview(textLabel) } } ```
0
0
553
Apr ’23
Bonjour discovery with NetServiceBrowser not working in iOS
i am using NetServiceBrowser for descovering bonjour services but my app is calling only netBrowserServiceWillSearch delegate method and keep on searching the Bonjour service without ever finding or stoping. class BonjourDiscovery : NetServiceBrowserDelegate, NetServiceDelegate { let serviceBrowser = NetServiceBrowser() @objc func ListOfPrinter(){ serviceBrowser.delegate = self serviceBrowser.searchForServices(ofType: "_printer._tcp", inDomain: "") } func netServiceBrowser(_ browser: NetServiceBrowser, didFind service: NetService, moreComing: Bool) { print("---------didFind: \(service.name)") } func netServiceBrowserDidStopSearch(_ browser: NetServiceBrowser) { print(#function) } func netServiceBrowser(_ browser: NetServiceBrowser, didNotSearch errorDict: [String : NSNumber]) { print(#function) } func netServiceBrowser(_ browser: NetServiceBrowser, didRemove service: NetService, moreComing: Bool) { print(#function) } func netServiceBrowserWillSearch(_ browser: NetServiceBrowser) { print(#function) } } info.plist -: <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>NSLocalNetworkUsageDescription</key> <string>Looking for local tcp Bonjour service</string> <key>NSBonjourServices</key> <array> <string>_printer._tcp</string> </array> </dict> </plist> i have tried diffrent things whatever i found on apple forum or stackOverFlow like adding local in inDomain ,replacing _printer._tcp with _printer._tcp. ,but nothing works so far
1
1
968
Mar ’23
How to get the list of all the printer connected to ios device programmatically?
I want to print documents using UIPrintIntrectionController() and need to pass one of the available printer but i have to pass the printer programmatically instead of hardcoding the details of printer i tried using NetServiceBrower() but it is depreceted and resources are not available about it `let printController = UIPrintInteractionController.shared let printInfo = UIPrintInfo(dictionary: nil) printInfo.jobName = "printing" printInfo.outputType = UIPrintInfo.OutputType.general printController.printInfo = printInfo let str = "hello" let dataStr: Data = Data(str.utf8) let data = NSData(data: dataStr) printController.printingItem = data //need to access a printer to pass to printerController let printer = ?? printController.print(to: printer)
0
1
798
Mar ’23