Category: swift

Swift is a general-purpose, multi-paradigm, compiled programming language developed by Apple Inc. and the open-source community. First released in 2014, Swift was developed as a replacement for Apple’s earlier programming language Objective-C, as Objective-C had been largely unchanged since the early 1980s and lacked modern language features.

  • NSTableView custom highlight color with NSTableRowView

    Getting the right color when you select NSTableView’s row was frustrating for me since I didn’t know how to do it properly. But it is really simple, all you need to do is having a class inherit from NSTableRowView and override its method drawSelection(in dirtyRect: NSRect).

    import Foundation
    import Cocoa
    
    class MenuTableRowView: NSTableRowView {
    
        override func drawSelection(in dirtyRect: NSRect) {
            if self.selectionHighlightStyle != .none {
                let selectionRect = NSInsetRect(self.bounds, 2.5, 2.5)
                
                if let color = NSColor.init(named: NSColor.Name("menu_table_selection_color")) {
                    color.setFill()
                }
                
                let selectionPath = NSBezierPath.init(roundedRect: selectionRect, xRadius: 0, yRadius: 0)
                selectionPath.fill()
            }
        }
        
    }
    

    Change menu_table_selection_color value in Assets.xcassets file.

    Custom NSTableRowView

    Now go to your NSTableViewDelegate’s method add your custom NSTableRowView.

     func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
            return MenuTableRowView()
    }

    Here you can see how my Storyboard looks like. Table view highlight value must be regular.

    Spread the love
  • This app is not allowed to query for scheme fb in iOS

    This app is not allowed to query for scheme fb in iOS

    I wanted to open a Facebook Group page in the Facebook app on my iPhone but I was getting that error since I did not have any LSApplicationQueriesSchemes in my Info.plist file.

    This is the code

    let fbPage = "fb://profile/" + FB_GROUP_ID // Your Facebook group id e.g 8794808798015
    let fbURL = URL(string:fbPage)
    let canOpenURL = UIApplication.shared.canOpenURL(fbURL!)
    if (canOpenURL) {
        UIApplication.shared.open(fbURL!, completionHandler: nil)
    }

    After adding ‘fb’ in my scheme it solves the issue.

    My Info.plist file

    This app is not allowed to query for scheme
    Spread the love
  • Navigation in SwiftUI with multiple views

    Navigation in SwiftUI with multiple views

    Download Complete Login app with Laravel API

    I was trying to build a simple app in iOS + SwiftUI with multiple views that uses NavigationView and NavigationLink.

    This app is very simple and uses a model to navigate between destinations. Enum ViewType tracks all the destinations. If you want to have another view, View4, just add a new case in ViewType enum.

    struct Menu: Identifiable {
        var id = UUID()
        var name: String
        var image: String
        var destination: ViewType
    }
    
    enum ViewType {
        case home
        case view1
        case view2
    }

    Our all views must have a property with menu type

    struct HomeView: View {
        var menu: Menu
        
        var body: some View {
            Text("Home View")
        }
    }

    Our MasterView which is the main view in root ContentView. Also, we added HomeView as our default view here.

    var body: some View {
            NavigationView {
                MasterView()
                    .navigationBarTitle(
                        Text("Navigation Demo")
                    )
                HomeView(menu: Menu(name:"Home",      image:"image",  destination:    .home))
            }.navigationViewStyle(DefaultNavigationViewStyle())
      }

    In our MasterView we have an array of Menu with title and destination view case, which populated in a list view.

    let  view1 =  Menu(name:"Home",      image:"image",  destination:    .home)
    let  view2 =  Menu(name:"View 1",          image:"image",  destination:    .view1)
    let  view3 =  Menu(name:"View 3",       image:"image",  destination:    .view2)
    
    var body: some View {
            let menus: [Menu] = [view1, view2, view3]
    
            return List {
                ForEach(menus) { menu in
                    self.destinationView(menu: menu)
                }
            }
    }

    function destinationView return a NavigationLink based on what item you choose on the list and take the user to the expected view.

    func destinationView(menu: Menu) -> some View {
            
            switch menu.destination {
                case .view1:
                    return NavigationLink(
                        destination: AnyView(View1(menu: menu))
                    )
                    {
                        Text("\(menu.name)")
                    }
                case .view2:
                       return NavigationLink(
                           destination: AnyView(View2(menu: menu))
                       )
                       {
                           Text("\(menu.name)")
                       }
                default:
                   return NavigationLink(
                       destination: AnyView(HomeView(menu: menu))
                   )
                   {
                       Text("\(menu.name)")
                   }
            }
            
        }

    Download the source code

    Spread the love