ios7 4S和IOS8 5S的AutoLayout设计

时间:2014-10-20 09:12:41

标签: ios objective-c ios7 ios8 autolayout

我为我的客户开发了一个应用程序,我的客户端和我正在使用iPhone 4s(IOS7)。最近我的客户在5c(IOS8)中使用了我的代码。现在有各种各样的问题。现在我就这些问题开始研发。 有人建议我使用AutoLayout设计UI。在我的项目中,我没有使用XIB,我只是以编程方式进行,我与XIbs没有联系。所以现在我开始以编程方式在AutoLayout上进行R& D.但我无法理解它(AutoLayout编程)。有什么建议吗?

IOS 7 4S

enter image description here

IOS8 5S

enter image description here

2 个答案:

答案 0 :(得分:1)

首先做一些阅读,以了解autolayout实际上是什么以及应该如何使用它。看一看here和/或观看this。他们都使用故事板,但他们可以帮助您理解自动布局的概念。在Ray Wederlich的网站上,您还可以找到有关如何实用地使用autolayout的其他文档,但我认为您必须为此付费。这是值得的,但您也可以免费在线找到其他帮助。

我遵循的基础是:

  • 创建视图

    UIView *myView = [UIView new];
    
  • 设置translatesAutoresizingMaskIntoConstraints属性

    myView.translatesAutoresizingMaskIntoConstraints = NO;
    
  • 将视图添加为子视图

    [self.view addSubview:myView];
    
  • 添加约束

    NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:myView 
                             attribute:NSLayoutAttributeTop 
                             relatedBy:NSLayoutRelationEqual
                                toItem:self.view 
                             attribute:NSLayoutAttributeTop
                            multiplier:1.0 
                              constant:0.0];
    
    [self.view addConstraint:constraint];
    

您需要添加多个约束,以便根据需要完全按照其他视图布局视图。

我还建议您创建自己的类别,以便在阅读时更快速,更容易理解编写自动布局代码。例如:PureLayout

如果你能够更具体地提出你的问题,可能会给你一些更好的示例代码,但你的问题太大而且范围广泛,所以请听听这个答案以及其他人给出的建议并做一些关于这个主题的阅读。首先制作一个简单的项目,然后使用autolayout布局一些视图,然后随着您越来越自信,构建更复杂的视图。

答案 1 :(得分:1)

如果您想以编程方式使用auto layout使用此link而另一个链接为here

这包含NSLayout文件下 UIKIT 框架中提供的所有NSLayoutConstraint.h枚举类型。

typedef NS_ENUM(NSInteger, NSLayoutAttribute) {
NSLayoutAttributeLeft = 1,
NSLayoutAttributeRight,
NSLayoutAttributeTop,
NSLayoutAttributeBottom,
NSLayoutAttributeLeading,
NSLayoutAttributeTrailing,
NSLayoutAttributeWidth,
NSLayoutAttributeHeight,
NSLayoutAttributeCenterX,
NSLayoutAttributeCenterY,
NSLayoutAttributeBaseline,

NSLayoutAttributeNotAnAttribute = 0
};