2016-01-06 80 views
0

我已經編程創建了UIScrollview。我已經在這個滾動視圖中添加了一些編程創建的按鈕。按鈕高度爲30.但按鈕寬度根據屏幕大小而變化。瞭解長寬比約束和乘數

float scrollYearX=btnLeftArrow.frame.origin.x+btnLeftArrow.frame.size.width+15; 
float scrollYearY=40.0; 
float scrollYearWidth=btnRightArrow.frame.origin.x-15-scrollYearX; 
float scrollYearHeight=50.0; 
scrollYear=[[UIScrollView alloc] initWithFrame:CGRectMake(scrollYearX, scrollYearY, scrollYearWidth, scrollYearHeight)]; 

[scrollYear setShowsHorizontalScrollIndicator:NO]; 
[scrollYear setShowsVerticalScrollIndicator:NO]; 

[vwParent addSubview:scrollYear]; 
int buttonX=0; 

for (int i=0; i<100; i++) { 
    UIButton *btn=[[UIButton alloc] initWithFrame:CGRectMake(buttonX, 0, scrollYearWidth/5, 30)]; 
    [btn.titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:14.0]]; 

    int mod=i%2; 
    if (mod==1) { 
     [btn setTitle:@"-" forState:UIControlStateNormal]; 
     [btn.titleLabel setTextColor:[UIColor whiteColor]]; 

    } 
    else 
    { 
     [btn addTarget:self action:@selector(yearClick :) forControlEvents:UIControlEventTouchUpInside]; 
     [btn setTitle:[NSString stringWithFormat:@"%li",(long)backYear] forState:UIControlStateNormal]; 
     if (backYear==year) { 
      btnCurrentYear=btn; 
      [btn setBackgroundImage:[UIImage imageNamed:@"yearSelect"] forState:UIControlStateNormal]; 
     } 
     backYear++; 
    } 
    [scrollYear addSubview:btn]; 
    btnLast=btn; 
    buttonX=buttonX+btn.frame.size.width; 
} 
[scrollYear setContentSize:CGSizeMake(btnLast.frame.origin.x+btnLast.frame.size.width, scrollYearHeight)]; 
[scrollYear setContentOffset:CGPointMake(btnCurrentYear.frame.origin.x-(btnLast.frame.size.width*2),0)]; 

我的問題是我應該使用哪種約束在每個設備中正確顯示。在這裏我附上了它如何在6s和ipad pro上顯示。我怎樣才能讓ipad pro的視覺效果與6s相同?在6S

enter image description here

按鈕

按鈕loks看起來在ipad親 enter image description here

請幫助我。 謝謝

回答

0

這是因爲你的按鈕背景寬度得到縮放。您需要使用可調整大小的圖像來保持圓形邊緣。改變你的背景圖片如下:

UIImage* backgroundImage = [UIImage imageNamed:@"yearSelect"]; 
backgroundImage = [backgroundImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, backgroundImage.size.width/2-1, 0, backgroundImage.size.width/2)]; 
[btn setBackgroundImage:backgroundImage forState:UIControlStateNormal]; 
+0

你是什麼意思的可調整大小的圖像?我該如何使用它們? – user1960169

+0

@ user1960169請參閱UIImage API,應該有像resizableImage這樣的方法。現在我使用iPhone客戶端,無法檢查文檔。 – SolaWing

+0

@ user1960169我添加了示例代碼。你可以嘗試一下。 – SolaWing