2015-11-23 115 views
0

我想優化應用程序尋找不同屏幕尺寸的方式以及我在不知何故在.xib中執行任何操作時我無法防止從切換時發生一些佈局錯位6splus-> 6s-> 5 iPhone模擬器。我試圖修復從我的標籤到圖像視圖(用作背景)的空間,然後根據設備縮放字體。儘管我設置了約束條件,但它們有些搞砸了,並沒有從一個屏幕大小實現到另一個屏幕大小。iOS,佈局位置取決於屏幕尺寸問題

6splus看起來是這樣的:

enter image description here

在這裏你可以看到,定位是即使右間距設置在.xib

然後右側完全錯誤的,要6S:

enter image description here

看起來RELAT (因爲我最初優化了它6秒)

注意它是如何從右向左移動的。我絕對想阻止它發生。

然後,iPhone 5:

enter image description here

完全轉移到左。不知何故,紅色的星期幾和日數之間的間距也增加了。這是我真正想要避免發生的第二件事。第三件事是左邊底部的標籤:它正在轉移一點點,我需要在那裏放置兩個標籤,所以避免它轉移也是非常重要的。

這裏是我的.xib與約束截圖:

enter image description here

.xib是定製UITableViewCell,我使用UITableView

以下圖片由標籤顯示的約束條件:

enter image description here

enter image description here

enter image description here

enter image description here

的UIImageView(背景)的約束:

enter image description here

任何建議將不勝感激。

+0

哪個項目被限制爲前導邊緣或後邊緣? 「活動日數量」僅限於位置和星期幾標籤的中心位置。其中之一是否受限於領先/尾隨? – Paulw11

+0

我更新了附加屏幕截圖的問題,請大家看看。另外,對於左下角的標籤,我希望避免移位。 –

+0

我也在動態地改變字體,但在這裏並不重要,因爲即使我爲所有的屏幕尺寸設置了相同的事情。 –

回答

0

所以,我最終做的是建立4個出口爲4個不同的約束條件:

1. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *eventNameBottomMargin; 
2. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *eventNameLeftMargin; 
3. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomMarginConstraint; 
4. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *rightMarginConstraint; 

約束1,2都爲UILabel左下方;限制3,4爲UILabels在右邊:使用Tj3n的建議我把它們放在UIView,所以然後3,4是UIView相對於背景UIImageView的限制。

然後我在代碼中根據設備寬度變化的約束值:

- (void)awakeFromNib 
{ 
[super awakeFromNib]; 
CGRect screenRect = [[UIScreen mainScreen] bounds]; 
NSNumber *screenWidth = @(screenRect.size.width); 
[self setFontsOnDeviceWithScreenWidth:screenWidth]; 

int rightMargin = 15; 
int bottomMargin = -105; 
int nameLeftMargin = 55; 
int nameBottomMargin = -33; 

if (screenWidth.intValue == 320) { 
    // iPhone 5 screenWidth = "320" 
    rightMargin = 5; 
    bottomMargin = -105; 
    nameLeftMargin = 50; 
    nameBottomMargin = -36; 
} else if(screenWidth.intValue == 375) { 
    // iPhone 6s 
    rightMargin = 15; 
    bottomMargin = -105; 
    nameLeftMargin = 80; 
    nameBottomMargin = -30; 
} else if(screenWidth.intValue == 414) { 
    // iPhone 6s Plus 
    rightMargin = 35; 
    bottomMargin = -108; 
    nameLeftMargin = 100; 
    nameBottomMargin = -30; 
} 
self.rightMarginConstraint.constant = rightMargin; 
self.bottomMarginConstraint.constant = bottomMargin; 
self.eventNameLeftMargin.constant = nameLeftMargin; 
self.eventNameBottomMargin.constant = nameBottomMargin; 
} 

我不認爲這是問題的最佳解決方案,但它是可以正常使用。應該有一種方法來指定約束,然後調整大小會自動進行,所以如果有人知道,請分享進一步的想法。