Y = ax + b
stackView seen same screen
Compression Resistance
Label, TextField, Button, Slider, ImageView
ソフトウェアエンジニアの技術ブログ:Software engineer tech blog
随机应变 ABCD: Always Be Coding and … : хороший
Y = ax + b
stackView seen same screen
Compression Resistance
Label, TextField, Button, Slider, ImageView
Intrinsic Content Size
Compression Resistance
Content Hugging
-stack view
axis, spacing, alignment, distribution
happily modifies size of view
-> fill(default), fill equally, fill proportionally
works hard not to modify size of views
->equal spacing, equal centering
alignment -horizontal stack views
text-> ascender, baseline, descender
guiding principles of autolayout
-stackView first, constraints later
-Start small, or not at all
-Work from the inside out
-Trust the simulator only
-Don’t panic
Seven Commandments of AutoLayout
1. Tweak the properties of the StackView
2. Use another StackView
3. Tweak the compression resistance or the hugging priority
4. Add constraints to the StackView
5. Add constraints to the views
6. To connect views within different StackViews, use V.
7. If everything else fails, use a filler view
-Laying out the views
-Deciding how these views will adapt when the available screen size changes
import UIKit class CodeViewController: UIViewController { override func loadView(){ code } }
-screen size changes
iphone se, iphone 6+
rotaion
code, xibs, storyboards
-Swift Code(NSLayoutConstraint)
-Visual Format Code
-Creating constraints in IB
UIStackView
create a project
add views
define layouts
integrate functions
connect ui to code
respond to events
func firstCharacter(of word: String) -> Character { return word[word.startIndex] } firstCharacter(of: "Mom")
import Fundation func placeFirstLetterLast(myString: String){ }
func placeFirstLetterLast (myString: String) -> String { var myString = myString myString.append(firstCharacter(of: myString)) return myString } func shortNameFromName(name: String) -> String { let lowercaseName = name.lowercaseString let vowelSet = CharacterSet(charactersIn: "aeiou") } shortNameFromName(name: "Nate") // shortNameFromName(name: "Talia") //
func lyricsForName(lyricsTemplate: String, fullName: String) -> String { let shortName = shortNameForName(name: fullName) let lyrics = lyricsTemplate .replacingOccurrences(of: "<FULL_NAME>", with: fullName) .replacingOccurrences(of: "<SHORT_NAME>", with: shortName) return lyrics }
The function print, min, max, and abs are a few examples of global functions. Check out a complete list of Swift’s global functions.
print("I'm a global function!") var initialPrice = 50 var bestOffer = 45 var finalPrice = min(bestOffer, initialPrice)
Anatomy of a function
func functionName (_ parameterName: parameterType) -> returnType { statements to execute return object }
func sumOfStrings(_ aBunchOfStrings: [String]) -> Int { let array = aBunchOfStrings var sum = 0 for string in array { if Int(string) != nil { let int intToAdd = Int(string)! sum += intToAdd } } return sum }
func reverse(_ string: String) -> String { var reversedString = "" for character in string.characters { reversedString = "\(character)" + reversedString } return reversedString }
func warmUp(temperature: Int) -> Int { return temperature + 10 } func firstChar(word: String) -> Character { return word[word.startIndex] } func concatenate(firstString: String, secondString secondString){ return firstString + secondString } func warmUp(temperature: Int) -> Int { return temperature + 10 } func firstCharacter(word: String) -> Character { return word[word.startIndex] } func concatenate(firstString: String, secondString: String)-> String { return firstString + secondString }
var lightSwitchOn: Bool = true var dimmer: Int = 7 var dimmerWithDecimals: Float = 3.14 var veryPreciseDimmer: Double = 3.14159265359 print(type(of: lightSwitchOn)) print(type(of: veryPreciseDimmer))
Bool Double
Double, Bool, String, Float
use let to declare a constant when we expect its value to remain the same
we expect let not change
let encouragement = "You can do it!" var personalizedEncouragement = "You can do it, Stephanie!" personalizedEncouragement = personalizedEncouragement.replacingOccurrences(of: "Stephanie", with: "Ryder") let birthYear = 2008 var currentYear = 2016 var age = currentYear - birthYear age = currentYear - birthYear
var word = "fortunate" word.contains("tuna") var password = "Mary had a little loris" var newPassword = password.replacingOccurences(of: "a" with "A")
import Foundation var didYouKnowString = "Did you know that the Swift String class comes with lots of useful methods?" var whisperString = "pass" + ", " + "\(didYouKnowString.lowercased())" var shoutString = "TODO: Make the shoutString here!"
import Foundation var forwardString = "stressed" var lottaLikes = "If like, you wanna learn Swift like, you should build lots of small apps, cuz it's like, a good way to practice."
import Glibc var nounArray = ["puppy", "laptop", "ocean", "app", "cow", "skateboard", "developer", "coffee", "moon"] var index1 = Int(random() % 9) var index2 = Int(random() % 9) var sentence = "The \(nounarray[6]) spilled her \(nounArray[7])." var sillySentence = "The \(nounArray[index1]) jumped over the \(nounArray[index2])." print(sentence) print(sillySentence)
var eString = "Meet me in St. Louis" for character in eString.characters { if character == "e" { print("found an e!") } else { } }
var eString = "Meet me in St. Louis" for character in eString.characters { if character == "e" { print("found an e!") } else { } } var theTruth = "Mony can buy me love." var characterCount = theTruth.characters.count var forwardString = "spoons" var charactersReversed = forwardString.characters.reversed() var backwardsString = String(charactersReversed) print(backwardsString)
found an e! found an e! found an e! snoops
var exitement: Character = "!" var myFirstString: String = "In wine, there is wisdom." var mySecondString: String = "In water, there is Giardia." var characterPoorString = "" var stringWithPotential = String() var theTruth = myFirstString + " " + mySecondString print(theTruth)
// Plain string var doggyDiet = "Mia eats 10lbs of dog food per month" // define variables var dogName = "Mia" var lbsPerMonth: Double = 10 // Same string, this time with the variable inserted doggyDiet = "\(dogName) eats \(lbsPerMonth)lbs of dog food per month" print(doggyDiet) let kilosInALb = 0.45 lbsPerMonth = 25 var metricDoggyDiet = "\(dogName) eats \(kilosInAlb * lbsPerMonth)kilos of dog food per month" print(metricDoggyDiet) dogName = "Mia" lbsPerMonth = 10 metricDoggyDiet = "\(dogName) eats \(kilosInALb * lbsPerMonth)kilos of dog food per month" print(metricDoggyDiet)