簡體   English   中英

如何使用iOS在故事板上創建ImageView中心?

[英]How to create ImageView center on storyboard using iOS?

我正在嘗試使用Xcode 7和iOS 9中的iOS故事板創建通用應用程序。在故事板概念中,我可以看到許多方法(自動布局,自動約束,自適應UI等)。 我對這些設計方法感到困惑。 請給我一個解決方案,為所有iPhone和iPad的中心圖像視圖顯示。

很容易..

1.將uiimageview置於中心位置

然后添加Below約束

1.centre水平到視圖&&中心垂直於視圖

2.將其設置為寬高比

請參見下面的圖片。 在此輸入圖像描述

在此輸入圖像描述

在此輸入圖像描述

中心水平,中心垂直,縱橫比設置這三個你會得到...

您可以嘗試使用許多不同屏幕尺寸的尺寸類。

改變這樣或那樣的約束

如果你想在iOS中設計通用應用程序,那么你必須根據percentage(%)添加約束。很少有情況或情況你不能從Starboard添加約束但不要擔心我們可以做到這一點編程。

你有沒有嘗試過KVConstraintExtensionsMaster庫來應用我已經實現的約束。 該庫具有updateapplyaccessremove通過Programmatically或從Interface Builder(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.在ViewController視圖中拖放imageView。

步驟2.現在在Container中添加約束Horizo​​ntal Center和在Container中添加Vertical Center。

步驟3.添加約束Equal Width和Equal Height。

步驟4.現在更新Equal Width約束和Equal Height的乘數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM