我正在尝试使用Xcode 7和iOS 9中的iOS故事板创建Universal应用程序。在故事板概念中,我可以看到很多方法(自动布局,自动约束,自适应UI等)。我对这些设计方法感到困惑。请为我提供一个解决方案,使图像视图在所有iPhone和iPad上居中显示。
我正在尝试使用Xcode 7和iOS 9中的iOS故事板创建Universal应用程序。在故事板概念中,我可以看到很多方法(自动布局,自动约束,自适应UI等)。我对这些设计方法感到困惑。请为我提供一个解决方案,使图像视图在所有iPhone和iPad上居中显示。
如果您想在iOS中设计通用应用程序,则必须基于百分比(%)
添加约束条件。有一些情况或情况,您无法从Starboard添加约束条件,但不要担心,我们可以通过编程来实现。
您是否尝试过{{link1:KVConstraintExtensionsMaster
}}库来应用我已经实现的约束条件?该库具有方法,可以更新
,应用
,访问
和删除
应用的约束条件,无论是通过编程方式
还是从界面生成器(StoryBoard / xib)
应用的约束条件。
以编程方式在所有iPhone和iPad上居中显示imageView。
- (void)viewDidLoad
{
[super viewDidLoad];
/* preparing View Hierarchy */
UIImageView *imageView = [UIImageView prepareNewViewForAutoLayout];
[self.view addSubview:imageView];
[imageView setBackgroundColor:[UIColor blueColor]];
/* applying constraints */
[imageView applyConstraintForCenterInSuperview];
/* Change the Ratio 0.0 to 1.0 to vary the height and width */
[imageView applyEqualHeightRatioPinConstrainToSuperview:0.8];
[imageView applyEqualWidthRatioPinConstrainToSuperview:0.8];
/* Comment the above both RatioPinConstrainToSuperview methods and uncomment below methods and run on both iPhone or iPad and see the magic.*/
/*
[imageView applyWidthConstraint:280]; // fixed width for iphone
[imageView applyHeightConstrain:280]; // fixed height for iphone
// update width constraint with proper iPad ratio
[imageView updateAppliedConstraintConstantValueForIpadByAttribute:NSLayoutAttributeWidth];
// update height constraint with proper iPad ratio
[imageView updateAppliedConstraintConstantValueForIpadByAttribute:NSLayoutAttributeHeight];
*/
}
在Storyboard中,使imageView在所有iPhone和iPad上居中显示。
步骤1. 将imageView拖放到ViewController的视图中。
步骤2. 现在添加容器中的水平居中约束和垂直居中约束。
步骤3. 添加等宽度和等高度约束。
步骤4. 现在更新等宽度约束和等高度约束的乘数。