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适配的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:https://blog.csdn.net/zfpp25_/article/details/48179709
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/199702

相关文章

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

使用Python开发一个现代化屏幕取色器

《使用Python开发一个现代化屏幕取色器》在UI设计、网页开发等场景中,颜色拾取是高频需求,:本文主要介绍如何使用Python开发一个现代化屏幕取色器,有需要的小伙伴可以参考一下... 目录一、项目概述二、核心功能解析2.1 实时颜色追踪2.2 智能颜色显示三、效果展示四、实现步骤详解4.1 环境配置4.

Python使用smtplib库开发一个邮件自动发送工具

《Python使用smtplib库开发一个邮件自动发送工具》在现代软件开发中,自动化邮件发送是一个非常实用的功能,无论是系统通知、营销邮件、还是日常工作报告,Python的smtplib库都能帮助我们... 目录代码实现与知识点解析1. 导入必要的库2. 配置邮件服务器参数3. 创建邮件发送类4. 实现邮件

基于Python开发一个有趣的工作时长计算器

《基于Python开发一个有趣的工作时长计算器》随着远程办公和弹性工作制的兴起,个人及团队对于工作时长的准确统计需求日益增长,本文将使用Python和PyQt5打造一个工作时长计算器,感兴趣的小伙伴可... 目录概述功能介绍界面展示php软件使用步骤说明代码详解1.窗口初始化与布局2.工作时长计算核心逻辑3

VS配置好Qt环境之后但无法打开ui界面的问题解决

《VS配置好Qt环境之后但无法打开ui界面的问题解决》本文主要介绍了VS配置好Qt环境之后但无法打开ui界面的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目UKeLvb录找到Qt安装目录中designer.UKeLvBexe的路径找到vs中的解决方案资源

python web 开发之Flask中间件与请求处理钩子的最佳实践

《pythonweb开发之Flask中间件与请求处理钩子的最佳实践》Flask作为轻量级Web框架,提供了灵活的请求处理机制,中间件和请求钩子允许开发者在请求处理的不同阶段插入自定义逻辑,实现诸如... 目录Flask中间件与请求处理钩子完全指南1. 引言2. 请求处理生命周期概述3. 请求钩子详解3.1

如何基于Python开发一个微信自动化工具

《如何基于Python开发一个微信自动化工具》在当今数字化办公场景中,自动化工具已成为提升工作效率的利器,本文将深入剖析一个基于Python的微信自动化工具开发全过程,有需要的小伙伴可以了解下... 目录概述功能全景1. 核心功能模块2. 特色功能效果展示1. 主界面概览2. 定时任务配置3. 操作日志演示

JavaScript实战:智能密码生成器开发指南

本文通过JavaScript实战开发智能密码生成器,详解如何运用crypto.getRandomValues实现加密级随机密码生成,包含多字符组合、安全强度可视化、易混淆字符排除等企业级功能。学习密码强度检测算法与信息熵计算原理,获取可直接嵌入项目的完整代码,提升Web应用的安全开发能力 目录

QT6中绘制UI的两种方法详解与示例代码

《QT6中绘制UI的两种方法详解与示例代码》Qt6提供了两种主要的UI绘制技术:​​QML(QtMeta-ObjectLanguage)​​和​​C++Widgets​​,这两种技术各有优势,适用于不... 目录一、QML 技术详解1.1 QML 简介1.2 QML 的核心概念1.3 QML 示例:简单按钮

在 PyQt 加载 UI 三种常见方法

《在PyQt加载UI三种常见方法》在PyQt中,加载UI文件通常指的是使用QtDesigner设计的.ui文件,并将其转换为Python代码,以便在PyQt应用程序中使用,这篇文章给大家介绍在... 目录方法一:使用 uic 模块动态加载 (不推荐用于大型项目)方法二:将 UI 文件编译为 python 模