Showing posts with label Swift for base. Show all posts
Showing posts with label Swift for base. Show all posts

Thursday, February 21, 2019

App doesn’t run because of breakpoint

Sometime your app does not run even if it has no error. Let look at left side Xcode, if you see something like this.


It is a Breakpoint you created by incident.
Right click mouse in to that breakpoint, delete it and project will run normaly.

Make Lauchscreen icon for ios app

This icon will show in 1-2 secons when app start.
At app tree folder, click to LaunchScreen.storyboard, it already has a View Controller inside.

Go to Object library on bottom right, drag an imageView in to storyboard.

Drag icon want to show in to project, go to Attributes inspector, Images, choose image name you want.

Click to image, go to triangle symbol at bottom, choose Add Missing Constraints.

Run to see flash image show.

Center a view

To make text center label, we use this line
la.textAlignment = NSTextAlignment.Center
To make label center screen, use this
ima.center = CGPoint(x: view.center.x, y:250)
There’s another way, first get screen width
let r = UIScreen.mainScreen().bounds.size.width
If label width 240, declare a variable name margin
let margin = (Int(r) - 240)/2
Use this variable to set label margin.
la = UILabel(frame: CGRect(x: margin, y: 60, width: 240, height: 25))
This way can use to every view when you know it’s width.

Swift array declare

If you declare array like this.
var ar = [Int]()
Then add elements
ar[0]=1
ar[1]=2
When get values, it show you error, we must use append to add elements.
ar.append(1)
If you declare array like this.
var ar=[Int](count: 8, repeatedValue: 0)
Now array will have 8 elements with the same value 0.
If you add elements like this.
ar.append(1)
Now number 1 become the ninth elements, if get value from 1-8 it still show zero number.
To avoid this, we add element like this.
ar[0]=1
If you declare array like this.
var ar = NSMutableArray(capacity: 15)
Now we can’t use append, we must add elements like this
ar[0]=1

ar[1]=3

Blank screen new project Xcode

Sometime you create new Xcode project, set text, color string but when running, nothing show in blank screen.
Go to Main.storyboard, choose Viewcontroller, on right top choose Attributes inspector, check to Is Initial View Controller.

When create new class, open it show black screen, add this line in to viewDidLoad.

view.backgroundColor = UIColor.whiteColor()