视图布局

import UIKit

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

self.window = UIWindow(frame: UIScreen.main.bounds)

self.window?.backgroundColor = #colorLiteral(red: 0.9098039269, green: 0.4784313738, blue: 0.6431372762, alpha: 1)

self.window?.makeKeyAndVisible()

self.window?.rootViewController = UIViewController()

/*

//(UIView Layout)视图布局

//frame bounds center

//frame决定的是一个视图在他父视图上的位置

let redView = UIView(frame: CGRect(x: 10, y: 20, width: 100, height: 200))

redView.backgroundColor = #colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1)

self.window?.addSubview(redView)

redView.frame.origin = CGPoint(x: 100, y: 200)  //位置变

redView.frame.size = CGSize(width: 200, height: 200) //大小变

//frame既能决定他在父视图的位置,也能控制它在父视图的大小

print(UIScreen.main.bounds)  //输出屏幕大小

//bounds  视图自身的边界,bounds决定自身子视图的位置,bounds的origin点默认和视图本事坐标系的原点是重合的

print(redView.bounds)

let greenView = UIView(frame: CGRect(x: 50, y: 50, width: 100, height: 100))

greenView.backgroundColor = #colorLiteral(red: 0.721568644, green: 0.8862745166, blue: 0.5921568871, alpha: 1)

redView.addSubview(greenView)

//不修改bounds的size,修改bounds的origin

print("中心点:\(redView.center)")

redView.bounds.origin = CGPoint(x: 50, y: 50)

print(greenView.frame)

//无论一个视图的bounds怎么改变,这个视图的中心点都不会改变

*/

//视图的层级关系

let a = UIView(frame: CGRect(x: 157, y: 200, width: 100, height: 100))

a.backgroundColor = #colorLiteral(red: 0.8549019694, green: 0.250980407, blue: 0.4784313738, alpha: 1)

self.window?.addSubview(a)

let b = UIView(frame: CGRect(x: 107, y: 150, width: 200, height: 200))

b.backgroundColor = #colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, alpha: 1)

self.window?.addSubview(b)

//谁最后添加谁就在最上层

//将视图a放在最上层显示

//self.window?.bringSubview(toFront: a)

//将视图b放在最下面

//self.window?.sendSubview(toBack: b)

//删除视图b

// b.removeFromSuperview()

//print(self.window?.subviews)  //显示window的所有视图

//将两个视图交换位置

self.window?.exchangeSubview(at: 1, withSubviewAt: 2)

return true

}

func applicationWillResignActive(_ application: UIApplication) {

// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.

}

func applicationDidEnterBackground(_ application: UIApplication) {

// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}

func applicationWillEnterForeground(_ application: UIApplication) {

// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.

}

func applicationDidBecomeActive(_ application: UIApplication) {

// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}

func applicationWillTerminate(_ application: UIApplication) {

// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}

}

你可能感兴趣的:(视图布局)