LEARN π | Simple examples of how to use SwiftUI
import SwiftUI
struct ContentView: View {
var body: some View {
//here is your code to build the view
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Same as ViewDidLoad()
A simple away to create table views
let listDataModel = [DataModel]()
NavigationView{
//create the tableView
List(listDataModel){ d in
//When table view cell is clicked
NavigationLink(
destination: DetailView()){
HStack{
Text(d.attribute)
}
}
}
.navigationTitle("Hack News") //here
}
Allows to identify each row of the list as unique. The struct need to have var id:Int
its a better delegate
struct Sender: ObservableObject{
@Published var message
}
struct Receiver{
@ObservedObject var recipient = Sender()
}
refactor the view when is changed
Create space between elements
Same as storyboard constraints
Allow to put the elements in horizontal alignment
Allow to put the elements in vertical alignment
Allow to overlap the elements
Set Color: Color(UIColor(red: 0.87, green: 0.60, blue: 0.40, alpha: 1.00)).edgesIgnoringSafeArea(.all)
Button(action: {
//Action
}, label: {
//Label Content
Text("Rool").font(.system(size: 50)).foregroundColor(.white).padding(.horizontal)
}).background(Color(#colorLiteral(red: 0.6097301841, green: 0.1105430648, blue: 0.1225960776, alpha: 1)))
Image("")
SF Symbols: Image(systemName: imageName).foregroundColor(Color(UIColor(red: 0.56, green: 0.22, blue: 0.30, alpha: 1.00)))
Overlay: Image(imageName)overlay(Circle().stroke(Color(UIColor(red: 0.36, green: 0.43, blue: 0.57, alpha: 1.00)),lineWidth: 5))
Text("")
Text("").font(Font.custom("Comfortaa", size: 40)) //custom font
Text("").font(.system(size: 25))
Text("").font(.title)
import WebKit
struct WebView : UIViewRepresentable{
let urlString:String?
func makeUIView(context: Context) -> WebView.UIViewType {
return WKWebView()
}
func updateUIView(_ uiView: WKWebView, context: Context) {
if let safeUrl = urlString {
if let url = URL(string: safeUrl){
let request = URLRequest(url: url)
uiView.load(request)
}
}
}
}
var body: some View {
ZStack{
Color(UIColor(red: 0.87, green: 0.60, blue: 0.40, alpha: 1.00)).edgesIgnoringSafeArea(.all)
VStack{
Image(imageName)
.resizable().aspectRatio(1,contentMode: .fit).frame(width: 150, height: 150).clipShape(Circle()).overlay(Circle().stroke(Color(UIColor(red: 0.36, green: 0.43, blue: 0.57, alpha: 1.00)),lineWidth: 5))
Text(name).font(Font.custom("Comfortaa", size: 40)).foregroundColor(.white)
Text(jobName)
.foregroundColor(.white)
.font(.system(size: 25))
Divider()
InfoView(text: phoneNumber, imageName: "phone.fill")
InfoView(text: email, imageName: "mail.fill")
}
} }
struct InfoView: View {
let text:String
let imageName:String
var body: some View {
RoundedRectangle(cornerRadius: 20).frame(height: 50).foregroundColor(.white).overlay(HStack{
Image(systemName: imageName).foregroundColor(Color(UIColor(red: 0.56, green: 0.22, blue: 0.30, alpha: 1.00)))
Text(text)
})
.padding(.all)
}
}
struct DiceView: View {
var n:Int
var body: some View {
Image("dice\(n)").resizable().aspectRatio(1, contentMode: .fit).padding()
}
}
struct ContentView: View {
@State var leftDiceNumber = 1
@State var rightDiceNumber = 1
var body: some View {
ZStack{
Image("background").resizable().edgesIgnoringSafeArea(/*@START_MENU_TOKEN@*/.all/*@END_MENU_TOKEN@*/)
VStack{
Image("diceeLogo")
Spacer()
HStack{
DiceView(n: leftDiceNumber)
DiceView(n: rightDiceNumber)
}.padding(.horizontal)
Spacer()
Button(action: {
leftDiceNumber = Int.random(in: 1...6)
rightDiceNumber = Int.random(in: 1...6)
}, label: {
Text("Rool").font(.system(size: 50)).foregroundColor(.white).padding(.horizontal)
}).background(Color(#colorLiteral(red: 0.6097301841, green: 0.1105430648, blue: 0.1225960776, alpha: 1)))
}.padding()
}
}
}
struct DetailView: View {
var url:String?
var body: some View {
WebView(urlString: url)
}
}
struct Post: Decodable, Identifiable{
var id:String{ //cause its a identifible
return objectID
}
let objectID:String
let points:Int
let title:String
let url:String?
}
struct Results:Decodable{
let hits: [Post]
}
struct DetailView: View {
var url:String?
var body: some View {
WebView(urlString: url)
}
}
class NetworkManager: ObservableObject{
@Published var posts = [Post]()
func fetchData(){
if let url = URL(string: "https://hn.algolia.com/api/v1/search?tags=front_page"){
let session = URLSession(configuration: .default)
let task = session.dataTask(with: url) { (data, response, error) in
if let e = error{
}else{
let decoder = JSONDecoder()
if let safeData = data{
do{
let results = try decoder.decode(Results.self,from: safeData)
DispatchQueue.main.async {
self.posts = results.hits
}
}catch {
print(error)
}
}
}
}
task.resume()
}
}
}
struct ContentView: View {
@ObservedObject var networkManager = NetworkManager()
var body: some View {
NavigationView{
List(networkManager.posts){ post in
NavigationLink(
destination: DetailView(url: post.url)){
HStack{
Text("\(post.points)").bold()
Text(post.title)
}
}
}
.navigationTitle("Hack News")
}
.onAppear(perform: {
self.networkManager.fetchData()
})
}
}