iOS开发~iPhone6及iPhone6P的UI适配

2023-10-13 01:10

本文主要是介绍iOS开发~iPhone6及iPhone6P的UI适配,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

概要

目前为止,iPhone屏幕尺寸已经有四种:

3.5(inch):1/3G/3GS/4/4S

4.0inch:5/5S/5C

4.7inch:6

5.5inch:6Plus


看一下iPhone4~6(+)的屏幕高宽比:

iPhone4(s):分辨率960*640,高宽比1.5
iPhone5(s):分辨率1136*640,高宽比1.775
iPhone6:分辨率1334*750,高宽比1.779
iPhone6+:分辨率1920*1080,高宽比1.778

可粗略认为iPhone5(s)、6(+)的高宽比是一致的(16:9),即可以等比例缩放。因此可以按宽度适配:
fitScreenWidth= width*(SCREEN_WIDTH/320)
这样,共有iPhone3/4/5、6、6+三组宽度,在iPhone6、6+下将按比例横向放大,也就是说我们要适配宽、高、字号大小(如果说Android屏幕适配是地狱一般,那目前来看iPhone屏幕适配还是很美好的)


适配思路

现在产品设计稿有以iPhone5为基准的,也有以iPhone6为基准的,其实没太大影响,因为iPhone5、6、6P的屏幕尺寸比例几乎一样的,所以以iPhone5为基准标注的尺寸,那适配的方法如下:

#define kScreenWidthRatio  (kScreenWidth / 320.0)
#define kScreenHeightRatio (kScreenHeight / 568.0)
#define AdaptedWidthValue(x)  (ceilf((x) * kScreenWidthRatio))
#define AdaptedHeightValue(x) (ceilf((x) * kScreenHeightRatio))
其实就是计算一个比例,然后iPhone6、6P等比放大,这样就保持了iPhone5、6、6P屏幕视觉效果上的一致了。

控件尺寸思路搞定了,但仅仅控件等比例拉伸,其中的内容也要去适应,例如UILabel的字体大小适应,其实也很简单:

#define kUHSystemFontWithSize(R)     [UIFont fontWithName: kULSystemFont size: (AdaptedWidthValue(R))]


实践
有了思路之后,实践一下看看效果,首先看一下最终目标效果图:



Demo简介:

1、利用TableView展示数据,其中TableView的headerView是滚动的广告,整体UI布局使用相对布局(Autolayout);

2、Autolayout用的是代码实现方式,借助与第三方库Masonry;

3、headerView的滚动广告实现是借助于第三方库SDCycleScrollView;

4、图片下载借助与第三方库SDWebImage;

5、UITableViewCell的自适应高度借助与第三方库UITableView+FDTemplateLayoutCell实现。


新建项目

使用Xcode新建项目后,由于使用到很多第三方,所以使用CocoPods,其中修改Podfile:

platform :ios, '7.0'
pod 'Masonry'
pod 'SDCycleScrollView'
pod 'UITableView+FDTemplateLayoutCell'
pod 'SDWebImage'

实现TableView

1、创建TableView,命名为newslistView:

@property (nonatomic, strong) UITableView *newslistView;

具体实现不说了,介绍一下TableView的布局,这里TableView沾满ViewController的View:

[self.newslistView mas_makeConstraints:^(MASConstraintMaker *make) {make.edges.equalTo(self.view);}];

2、实现TableViewHeader

- (void) loadTableViewHeaderView {SDCycleScrollView * cycleScrollView =  [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, kScreenWidth, AdaptedHeightValue(SDCycleScrollViewHeight)) imageURLStringsGroup:nil]; // 模拟网络延时情景cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;cycleScrollView.delegate = self;cycleScrollView.showPageControl = YES;cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;cycleScrollView.dotColor = [UIColor whiteColor]; // 自定义分页控件小圆标颜色cycleScrollView.placeholderImage = [UIImage imageNamed:@"detail_top"];[self.view addSubview:cycleScrollView];cycleScrollView.imageURLStringsGroup = [self.dataDictionary valueForKey:@"advertisement"];self.newslistView.tableHeaderView = cycleScrollView;
}

这里使用到了  AdaptedHeightValue ( SDCycleScrollViewHeight )来适应屏幕尺寸,4/4S设备的TableViewHeader高度就小一些,6和6P的TableViewHeader高度就大一些,因为我们是已5代设备为参考实现的产品设计稿。

3、实现TableViewCell

#define UI_DEBUG 0#define ULAppearanceFontSizeMiddle 13
#define ULAppearanceFontSizeSmall  12NSString  *const NewsListCellIdentifier = @"NewsListCellIdentifier";static const CGFloat ULNewsListCellNewsimageViewMarginLeft = 10.0;
static const CGFloat ULNewsListCellNewsimageViewWidth = 100.0;
static const CGFloat ULNewsListCellNewsimageViewHeight = 80.0;static const CGFloat ULNewsListCellTitleLabelMarginTop = 10.0;
static const CGFloat ULNewsListCellTitleLabelMarginLeft = 10.0;
static const CGFloat ULNewsListCellTitleLabelMarginRight = 10.0;
static const CGFloat ULNewsListCellTitleLabelHeight = 20.0;static const CGFloat ULNewsListCellContentLabelMarginTop = 10.0;
static const CGFloat ULNewsListCellContentLabelMarginBottom = 10.0;static const CGFloat ULNewsListCellLineViewMarginLeft = 10.0;
static const CGFloat ULNewsListCellLineViewMarginRight = 10.0;
static const CGFloat ULNewsListCellLineViewHeight = 0.5;@interface NewsListCell ()@property (nonatomic, strong) UIImageView *newsImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UIView *lineView;@end@implementation NewsListCell- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
#if UI_DEBUGself.contentView.backgroundColor = [UIColor redColor];
#endif[self.contentView addSubview:self.newsImageView];[self.contentView addSubview:self.titleLabel];[self.contentView addSubview:self.contentLabel];[self.contentView addSubview:self.lineView];[self makeConstraintSubviews];}return self;
}- (void) makeConstraintSubviews {[self.newsImageView mas_makeConstraints:^(MASConstraintMaker *make) {make.left.equalTo(self.contentView.mas_left).offset(AdaptedWidthValue(ULNewsListCellNewsimageViewMarginLeft));make.size.mas_equalTo(CGSizeMake(AdaptedWidthValue(ULNewsListCellNewsimageViewWidth), AdaptedHeightValue(ULNewsListCellNewsimageViewHeight)));make.centerY.equalTo(self.contentView.mas_centerY);}];[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {make.top.equalTo(self.contentView.mas_top).offset(AdaptedHeightValue(ULNewsListCellTitleLabelMarginTop));make.left.equalTo(self.newsImageView.mas_right).offset(AdaptedWidthValue(ULNewsListCellTitleLabelMarginLeft));make.right.equalTo(self.contentView.mas_right).offset(-AdaptedWidthValue(ULNewsListCellTitleLabelMarginRight));make.height.mas_equalTo(AdaptedHeightValue(ULNewsListCellTitleLabelHeight));
//        make.bottom.equalTo(self.contentLabel.mas_top).offset(-AdaptedHeightValue(ULNewsListCellContentLabelMarginTop));}];[self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {make.left.right.equalTo(self.titleLabel);make.top.equalTo(self.titleLabel.mas_bottom).offset(AdaptedHeightValue(ULNewsListCellContentLabelMarginTop));make.bottom.equalTo(self.lineView.mas_bottom).offset(-AdaptedHeightValue(ULNewsListCellContentLabelMarginBottom));}];[self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {make.bottom.equalTo(self.contentView.mas_bottom).offset(-ULNewsListCellLineViewHeight);make.left.equalTo(self.contentView.mas_left).offset(AdaptedWidthValue(ULNewsListCellLineViewMarginLeft));make.right.equalTo(self.contentView.mas_right).offset(-AdaptedWidthValue(ULNewsListCellLineViewMarginRight));;make.height.mas_equalTo(ULNewsListCellLineViewHeight);}];
}- (void)configureWithData:(News *) news {[self.newsImageView sd_setImageWithURL:[NSURL URLWithString:news.imageUrl]];self.titleLabel.text = news.title;self.contentLabel.text = news.content;
}#pragma mark - Getters- (UIImageView *) newsImageView {if (!_newsImageView) {_newsImageView = [[UIImageView alloc] init];
#if UI_DEBUG_newsImageView.backgroundColor = [UIColor greenColor];
#endif}return _newsImageView;
}- (UILabel *) titleLabel {if (!_titleLabel) {_titleLabel = [[UILabel alloc] init];_titleLabel.font = kUHSystemFontWithSize(ULAppearanceFontSizeMiddle);_titleLabel.textColor = [UIColor blackColor];#if UI_DEBUG_titleLabel.backgroundColor = [UIColor lightGrayColor];
#endif}return _titleLabel;
}- (UILabel *) contentLabel {if (!_contentLabel) {_contentLabel = [[UILabel alloc] init];_contentLabel.font = kUHSystemFontWithSize(ULAppearanceFontSizeSmall);_contentLabel.textColor = [UIColor grayColor];_contentLabel.numberOfLines = 0;_contentLabel.lineBreakMode = NSLineBreakByWordWrapping;_contentLabel.textAlignment = NSTextAlignmentLeft;#if UI_DEBUG_contentLabel.backgroundColor = [UIColor brownColor];
#endif}return _contentLabel;
}- (UIView *) lineView {if (!_lineView) {_lineView = [[UIView alloc] init];_lineView.backgroundColor = [UIColor lightGrayColor];}return _lineView;
}@end


这样利用尺寸拉伸,并配合Autolayout,就轻松实现了iPhone设备适配。其中Cell高度自动适应请参考 UITableView+FDTemplateLayoutCell第三方库使用方法。

如果不使用Autolayout,而使用Frame方式,这里就不介绍了,还是尽快转向Autolayout吧,还有一些相关内容也很好,想了解可以参考:

http://blog.csdn.net/phunxm/article/details/42174937

http://www.cocoachina.com/ios/20141230/10800.html


欢迎一起讨论


Demo下载


这篇关于iOS开发~iPhone6及iPhone6P的UI适配的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/199702

相关文章

Python38个游戏开发库整理汇总

《Python38个游戏开发库整理汇总》文章介绍了多种Python游戏开发库,涵盖2D/3D游戏开发、多人游戏框架及视觉小说引擎,适合不同需求的开发者入门,强调跨平台支持与易用性,并鼓励读者交流反馈以... 目录PyGameCocos2dPySoyPyOgrepygletPanda3DBlenderFife

使用Python开发一个Ditto剪贴板数据导出工具

《使用Python开发一个Ditto剪贴板数据导出工具》在日常工作中,我们经常需要处理大量的剪贴板数据,下面将介绍如何使用Python的wxPython库开发一个图形化工具,实现从Ditto数据库中读... 目录前言运行结果项目需求分析技术选型核心功能实现1. Ditto数据库结构分析2. 数据库自动定位3

Django开发时如何避免频繁发送短信验证码(python图文代码)

《Django开发时如何避免频繁发送短信验证码(python图文代码)》Django开发时,为防止频繁发送验证码,后端需用Redis限制请求频率,结合管道技术提升效率,通过生产者消费者模式解耦业务逻辑... 目录避免频繁发送 验证码1. www.chinasem.cn避免频繁发送 验证码逻辑分析2. 避免频繁

Spring Boot集成/输出/日志级别控制/持久化开发实践

《SpringBoot集成/输出/日志级别控制/持久化开发实践》SpringBoot默认集成Logback,支持灵活日志级别配置(INFO/DEBUG等),输出包含时间戳、级别、类名等信息,并可通过... 目录一、日志概述1.1、Spring Boot日志简介1.2、日志框架与默认配置1.3、日志的核心作用

PyQt5 GUI 开发的基础知识

《PyQt5GUI开发的基础知识》Qt是一个跨平台的C++图形用户界面开发框架,支持GUI和非GUI程序开发,本文介绍了使用PyQt5进行界面开发的基础知识,包括创建简单窗口、常用控件、窗口属性设... 目录简介第一个PyQt程序最常用的三个功能模块控件QPushButton(按钮)控件QLable(纯文本

WinForm跨线程访问UI及UI卡死的解决方案

《WinForm跨线程访问UI及UI卡死的解决方案》在WinForm开发过程中,跨线程访问UI控件和界面卡死是常见的技术难题,由于Windows窗体应用程序的UI控件默认只能在主线程(UI线程)上操作... 目录前言正文案例1:直接线程操作(无UI访问)案例2:BeginInvoke访问UI(错误用法)案例

基于Python开发一个图像水印批量添加工具

《基于Python开发一个图像水印批量添加工具》在当今数字化内容爆炸式增长的时代,图像版权保护已成为创作者和企业的核心需求,本方案将详细介绍一个基于PythonPIL库的工业级图像水印解决方案,有需要... 目录一、系统架构设计1.1 整体处理流程1.2 类结构设计(扩展版本)二、核心算法深入解析2.1 自

SpringBoot开发中十大常见陷阱深度解析与避坑指南

《SpringBoot开发中十大常见陷阱深度解析与避坑指南》在SpringBoot的开发过程中,即使是经验丰富的开发者也难免会遇到各种棘手的问题,本文将针对SpringBoot开发中十大常见的“坑... 目录引言一、配置总出错?是不是同时用了.properties和.yml?二、换个位置配置就失效?搞清楚加

Python中对FFmpeg封装开发库FFmpy详解

《Python中对FFmpeg封装开发库FFmpy详解》:本文主要介绍Python中对FFmpeg封装开发库FFmpy,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、FFmpy简介与安装1.1 FFmpy概述1.2 安装方法二、FFmpy核心类与方法2.1 FF

基于Python开发Windows屏幕控制工具

《基于Python开发Windows屏幕控制工具》在数字化办公时代,屏幕管理已成为提升工作效率和保护眼睛健康的重要环节,本文将分享一个基于Python和PySide6开发的Windows屏幕控制工具,... 目录概述功能亮点界面展示实现步骤详解1. 环境准备2. 亮度控制模块3. 息屏功能实现4. 息屏时间