2015-11-30 91 views
3

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

+0

請參閱[如何在故事板中居中查看](http:// stackoverflow。com/a/28446317/3681880) – Suragch

回答

4

其相當容易..

1.place所述的UIImageView居中

然後將下面的約束添加

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

2.設置長寬比

請參閱下圖。 enter image description here

enter image description here

enter image description here

中心水平,垂直居中,寬高比設置這三年你會得到...

+0

或者'而不是'長寬比'只需添加圖像,'imageView'將獲得圖像的大小。 –

+0

不,它不工作。顯示紅色標記,如果我清除了錯誤,它顯示不正確。@ Kishore! – android

+0

我已更新我的答案,請檢查它.... –

2

你可以試試大小班有許多不同的屏幕尺寸工作。

變化約束這樣或其他

+0

我只使用大小班。我正在使用的所有佈局的基準值。@ Klone – android

+0

變更約束@android – klone

0

如果你想設計在iOS的通用應用程序,那麼你必須添加的percentage(%)約束基礎和很少有情況或案件的您不能在Starboard中添加約束條件,但不要擔心,我們可以通過編程來實現。

你有沒有嘗試KVConstraintExtensionsMaster庫來應用我已經實現的約束。該庫具有方法,該方法將updateapplyaccessremove那些被施加由Programmatically或從Interface Builder(StoryBoard/xib)任一所施加的約束。

編程方式imageView顯示所有iPhone和iPad的中心。

- (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步。現在在Container中添加約束水平中心,並在容器中添加垂直中心。

第3步。添加限制等寬和等高。

第4步。現在更新等寬度約束和等高的乘數。

相關問題