2013-02-17 147 views
0

我加入的UIImageView到按鈕的方式,:自動佈局拉伸圖像,自動佈局,IOS

  1. 的UIImageView是沿着y軸(只是再次編輯)
  2. 結尾間隔中心的UIImageView的始終是10分從按鈕

,併爲此,我做類似下面的

// Just added these lines 
    UIImage *img    = [UIImage imageNamed:@"reload.png"]; 
    indicator     = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, img.size.width, img.size.width)]; 
    indicator.image    = img; 

    [self.logInButton addConstraints:[NSLayoutConstraint constraintsWithVisualFormat: @"H:|[indicator]-(10)-|" 
                       options:0 
                       metrics:nil 
                        views:NSDictionaryOfVariableBindings(indicator)]]; 

    [self.logInButton addConstraint:[NSLayoutConstraint constraintWithItem:self.logInButton 
                   attribute:NSLayoutAttributeCenterY 
                   relatedBy:NSLayoutRelationEqual 
                   toItem:indicator 
                   attribute:NSLayoutAttributeCenterY 
                  multiplier:1.0 
                   constant:0]]; 

然而,我所得到的是UIImageView的是伸出像下面

enter image description here

有沒有人對此有任何想法。所有評論在這裏讚賞。

回答

0

您的約束條件是說圖像視圖應該與左邊緣齊平並且距離右邊10個點,所以如果按鈕大於圖像的大小,圖像將伸展。您或者需要將其居中在x方向(並將右側的一個消除)或爲圖像視圖設置特定的大小。

+0

我仍然困惑,只是編輯了OP,請再看看。 – tranvutuan 2013-02-17 03:00:03

+0

@ttran,我不明白你的困惑。您的constraintWithItem代碼將它置於y軸上,即垂直,而不是水平。視覺格式在左邊緣(H:| [indicator]這一部分)和右邊緣10(本部分[indicator] - (10) - |)有10個。你需要擺脫視覺約束,並添加另一個像你的第二個,但與CenterX屬性。 – rdelmar 2013-02-17 03:45:33

+0

如果OP再次失去你,對不起。我的目標就像[this](http://cl.ly/Mwqz)。這就是爲什麼我使用centerY。就像你說的,如果我刪除視覺約束,我不會有一個位於按鈕右邊緣10的圖像。 – tranvutuan 2013-02-17 06:32:32