专门用于纵向 3.5 英寸(iPhone 4S)Xcode 6 的尺寸等级?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28818962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Size class specifically for portrait 3.5 inch (iPhone 4S) Xcode 6?
提问by Helen Wood
I'm tuning my UI App, but I got an issue that I can't solve.
我正在调整我的 UI 应用程序,但遇到了无法解决的问题。
As I can see Compact height affects all iPhones under 4.7 inches, but my UI is fine except for the iPhone 4S (3.5 inches).
正如我所看到的紧凑高度影响所有 4.7 英寸以下的 iPhone,但我的用户界面很好,除了 iPhone 4S(3.5 英寸)。
I don't want to modify the layout for all iPhones under 4.7 inches, just the iPhone 4S, at the same time I don't want to left out this device.
我不想修改所有 4.7 英寸以下 iPhone 的布局,只修改 iPhone 4S,同时我也不想遗漏这个设备。
There's any workaround so I can set the amendments but just and only for the 3.5 inches portrait? or should I say goodbye to 100 millions devices out there?
有什么解决方法,所以我可以设置修正但仅适用于 3.5 英寸肖像?还是我应该告别 1 亿台设备?
I know it's a tough question and almost an opinion poll, but technically speaking I would like to find my best way out here.
我知道这是一个棘手的问题,几乎是一个民意调查,但从技术上讲,我想在这里找到最好的出路。
采纳答案by Helen Wood
@all I can't make things smaller, because I'm dealing with pickerviews which happen to have only three valid heights for UIPickerView (162.0, 180.0 and 216.0). Sizes and constraints apart.
@all 我不能让事情变小,因为我正在处理 UIPickerView 只有三个有效高度(162.0、180.0 和 216.0)的选择器视图。大小和限制分开。
iPhone Sizes: http://www.idev101.com/code/User_Interface/sizes.html, 4S is unique.
iPhone 尺寸:http: //www.idev101.com/code/User_Interface/sizes.html,4S 是独一无二的。
So although my approach it's a little bit ugly get the things done, nearly on my point.
因此,尽管我的方法有点丑陋,但完成工作几乎符合我的观点。
So I know it's far from Goodville, don't hit me down, just for sharing:
所以我知道它离Goodville很远,不要打我,只是为了分享:
func checkForiPhone4S()
{
if (UIScreen.mainScreen().bounds.size.height == 480) {
println("It is an iPhone 4S - Set constraints")
listPickerView.transform = CGAffineTransformMakeScale(1, 0.8);
var constraintHeight = NSLayoutConstraint(item: listPickerView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100)
self.view.addConstraint(constraintHeight)
datePickerView.transform = CGAffineTransformMakeScale(1, 0.8);
var constraintHeightDate = NSLayoutConstraint(item: datePickerView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100)
self.view.addConstraint(constraintHeightDate)
}
}
回答by Pavel Alexeev
There is no size class for iPhone 3.5 inch.
iPhone 3.5 英寸没有尺寸等级。
So I've made a class category for NSLayoutConstraint to edit it in Interface Builder which is very easy to use:
所以我为 NSLayoutConstraint 创建了一个类类别,以便在 Interface Builder 中对其进行编辑,这非常易于使用:
@interface NSLayoutConstraint (Extensions)
@property (nonatomic) IBInspectable CGFloat iPhone3_5_Constant;
@end
–
——
@implementation NSLayoutConstraint (Extensions)
- (CGFloat)iPhone3_5_Constant
{
return self.constant;
}
- (void)setIPhone3_5_Constant:(CGFloat)iPhone3_5_Constant
{
if ([UIScreen mainScreen].bounds.size.height < 500) {
self.constant = iPhone3_5_Constant;
}
}
@end
回答by Peterdk
Swift 3 version of Pavel Alexeev's solution. In Swift you can't use stored properties in extensions, so we apply it directly to the constant
property.
Pavel Alexeev 解决方案的 Swift 3 版本。在 Swift 中,您不能在扩展中使用存储的属性,因此我们将其直接应用于constant
属性。
extension NSLayoutConstraint
{
//We use a simple inspectable to allow us to set a value for iphone 4.
@IBInspectable var iPhone4_Constant: CGFloat
{
set{
//Only apply value to iphone 4 devices.
if (UIScreen.mainScreen().bounds.size.height < 500)
{
self.constant = newValue;
}
}
get
{
return self.constant;
}
}
}
回答by Jake MacMullin
An approach that just worked for me was to use the same constraints for all compact size classes but to use a combination of a greater than or equal to constraint and priorities to modify how the views were positioned on the iPhone 4's smaller screen.
一种对我有用的方法是对所有紧凑尺寸类使用相同的约束,但使用大于或等于约束和优先级的组合来修改视图在 iPhone 4 较小屏幕上的定位方式。
I've got a constraint between the top of a numeric keypad view and its superview that is set to be greater than or equal to 160 (with a priority of 1000) and a constraint between the bottom of the keypad view and the bottom of the superview that is set to a constant of 30 (but with a lower priority of 750).
我在数字小键盘视图的顶部与其超级视图之间有一个约束,该约束设置为大于或等于 160(优先级为 1000),以及小键盘视图底部和底部之间的约束superview 设置为常量 30(但优先级较低,为 750)。
This means that on the iPhone 4 where there's not enough room for 160+ points above the keypad and 30 points below then it's the space below that goes.
这意味着在 iPhone 4 上没有足够的空间容纳键盘上方 160+ 点和下方 30 点的空间,然后是下方的空间。
Whilst this approach may not work in all cases, I'd encourage you to think about whether there's a set of priorities that will allow your views to adjust in the way you want on the smaller screen.
虽然这种方法可能不适用于所有情况,但我鼓励您考虑是否有一组优先级可以让您的视图在较小的屏幕上以您想要的方式进行调整。
回答by Poltavets
Swift 5.0 code for Pavel Alexeev's solution., accounting for some syntax updates and also screen width because I've found that if the device is being held in the landscape orientation when the app is launched, the screen height is not the actual portrait height, but the current landscape height. So, I check that the width is accounted for, too. If the height is less than 660 AND the width is less than 375, we have a portrait SE or 5s.
Pavel Alexeev 解决方案的 Swift 5.0 代码,考虑了一些语法更新和屏幕宽度,因为我发现如果在启动应用程序时设备处于横向,屏幕高度不是实际的纵向高度,但目前的景观高度。所以,我检查宽度是否也被考虑在内。如果高度小于 660 且宽度小于 375,则我们有纵向 SE 或 5s。
extension NSLayoutConstraint
{
//We use a simple inspectable to allow us to set a value for iphoneSE / 5s.
@IBInspectable var iPhoneSE_PortraitConstant: CGFloat
{
set{
//Only apply value to iphone SE and 5s devices.
if (UIScreen.main.bounds.size.height < 660 && UIScreen.main.bounds.size.width < 330)
{
self.constant = newValue;
}
}
get
{
return self.constant;
}
}
}