iOS开发框架UIKit
iOS开发框架UIKit

iOS开发框架UIKit

UIKit 是苹果公司为 iOS 系统提供的用户界面开发框架,用于构建 iPhone 和 iPad 应用的图形界面。

UIKit核心框架

1、UIApplication

代表整个应用程序的生命周期。

由系统自动创建,不需要手动实例化。

通常在 AppDelegateSceneDelegate 中配合使用,响应应用启动、进入后台、前台等状态。

2UIWindow

视图层级的最顶层容器。

每个 window 通常包含一个 rootViewController。

多用于多场景(Scene)或外部显示器支持。

3、UIView

所有界面元素的基础类,矩形区域。

可组合、嵌套、添加动画。

关键属性:

frame:相对于父视图的位置和大小。

bounds:自身坐标系的位置和大小。

center:中心点。

backgroundColor:背景色。

let redView = UIView(frame: CGRect(x: 50, y: 50, width: 100, height: 100))
redView.backgroundColor = .red
view.addSubview(redView)

4、UIViewController(控制器)

管理一组视图。

生命周期方法:

viewDidLoad():视图加载完成。

viewWillAppear() / viewDidAppear():即将/已经显示。

viewWillDisappear() / viewDidDisappear():即将/已经隐藏。

可以被模态弹出 (present) 或放入导航栈中。

常用子类:

UITableViewController:用于表格。

UICollectionViewController:用于网格。

UINavigationController:导航控制器。

UITabBarController:标签控制器。

UIKit基础组件

1、界面控制器:UIViewController

2、视图元素:UIView、UILabel、UIButton、UIImageView

3、表格/列表:UITableView、UICollectionView

4、导航与结构:UINavigationController、UITabBarController

5、动画:UIView.animate(…)

6、触摸处理:UITapGestureRecognizer、UIPanGestureRecognizer

7、窗口管理:UIWindow、UIApplication

UIKit和SwiftUI区别

1、编程方式:UIKit基于命令式代码(Imperative),SwiftUI基于声明式代码(Declarative)。

2、生命周期:UIKit更负责,管理控制器堆栈,SwiftUI自动管理状态和界面。

3、学习曲线:UIKit更复杂,更底层,SwiftUI更简单、更现代化。

4、使用时间:UIKit自2008年至今,成熟稳定。SwiftUI从iOS 13+(2019年)起推出,仍在发展。

代码示例:

// UIKit 的按钮
let button = UIButton(type: .system)
button.setTitle("点击我", for: .normal)
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
view.addSubview(button)

// SwiftUI 的按钮
Button("点击我") {
    print("按钮被点击")
}

总结

UIKit 是 iOS 上最经典、最成熟的 UI 框架,SwiftUI 是它的现代继任者。

如果需要开发 iOS 13+ 版本,推荐从SwiftUI开始学起,SwiftUI兼容macOS、WatchOS、小组件等系统。

还可以通过iOS App声明周期管理,了解UIKit各核心框架的管理内容。

相关文章    

1、iOS多用户界面场景UIWindowScene:https://fangjunyu.com/2025/05/21/ios%e5%a4%9a%e7%94%a8%e6%88%b7%e7%95%8c%e9%9d%a2%e5%9c%ba%e6%99%afuiwindowscene/

2、iOS支持多窗口的UIScene:https://fangjunyu.com/2025/05/21/ios%e6%94%af%e6%8c%81%e5%a4%9a%e7%aa%97%e5%8f%a3%e7%9a%84uiscene/

3、iOS App生命周期管理:https://fangjunyu.com/2025/05/20/ios-app%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f%e7%ae%a1%e7%90%86/

4、iOS窗口容器UIWindow:https://fangjunyu.com/2025/05/20/ios%e7%aa%97%e5%8f%a3%e5%ae%b9%e5%99%a8uiwindow/

5、iOS界面UIView:https://fangjunyu.com/2025/05/20/ios%e7%95%8c%e9%9d%a2uiview/

6、iOS代理类AppDelegate:https://fangjunyu.com/2025/05/20/ios%e4%bb%a3%e7%90%86%e7%b1%bbappdelegate/

7、iOS UIApplication类:https://fangjunyu.com/2025/05/20/ios-uiapplication%e7%b1%bb/

8、iOS管理多窗口的SceneDelegate:https://fangjunyu.com/2025/05/20/ios%e7%ae%a1%e7%90%86%e5%a4%9a%e7%aa%97%e5%8f%a3%e7%9a%84scenedelegate/

9、SwiftUI和iOS核心类UIViewController:https://fangjunyu.com/2025/05/19/swiftui%e5%92%8cios%e6%a0%b8%e5%bf%83%e7%b1%bbuiviewcontroller/

   

如果您认为这篇文章给您带来了帮助,您可以在此通过支付宝或者微信打赏网站开发者。

欢迎加入我们的 微信交流群QQ交流群,交流更多精彩内容!
微信交流群二维码 QQ交流群二维码

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注