2017-03-16 52 views
0

首先我已經以編程方式,而不是解決這個問題的佈局在故事板,因爲我已經通過編碼定義我的按鈕不拖&通過故事板下跌的,如何在標準UITableViewCell中以編程方式自動生成用於右側自定義UIButton的UITableViewCell。

我編程的通用iOS應用程序,我在其中很少有TableView的。我必須在每個UITableViewCell右側添加一些自定義按鈕,或者您可以說我必須在單元格的邊緣添加按鈕,它在模擬器的iPhone7 Plus中工作得很好,但是其他的iPhone 5或5s其他功能的其他功能&其他iOS設備?這裏是它的外觀在iphone7加&的iPhone 5S

enter image description here

什麼樣的適度我應該在有關佈局的程序(代碼)做的,這裏是我的代碼在UITableViewCell方法定義中的cellForRowAtIndexPath方法4個按鈕,

//Favorite Button 

    UIButton *addtoFavsButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
     [addtoFavsButton setImage:[UIImage imageNamed:@"like"] forState:UIControlStateNormal]; 
    addtoFavsButton.frame = CGRectMake(370.0f, 0.0f, 50.0f, 45.0f); 
    [cell addSubview:addtoFavsButton]; 
    [addtoFavsButton addTarget:self 
         action:@selector(addtoFavs:) 
       forControlEvents:UIControlEventTouchUpInside]; 
    [cell.contentView addSubview:addtoFavsButton]; 
    [addtoFavsButton setTag:indexPath.row]; 

// WebView Button 

UIButton *webViewButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[webViewButton setImage:[UIImage imageNamed:@"web"] forState:UIControlStateNormal]; 
webViewButton.frame = CGRectMake(335.0f, 0.0f, 50.0f, 50.0f); 
[cell addSubview:webViewButton]; 
[webViewButton addTarget:self 
        action:@selector(webView:) 
     forControlEvents:UIControlEventTouchUpInside]; 
[cell.contentView addSubview:webViewButton]; 
[webViewButton setTag:indexPath.row]; 


// Twitter Share Button 

UIButton *twitterButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[twitterButton setImage:[UIImage imageNamed:@"twitter"] forState:UIControlStateNormal]; 
twitterButton.frame = CGRectMake(300.0f, 0.0f, 45.0f, 42.0f); 
[cell addSubview:twitterButton]; 
[twitterButton addTarget:self action:@selector(twitterShare:event:) forControlEvents:UIControlEventTouchUpInside]; 
[cell.contentView addSubview:twitterButton]; 
//[twitterButton setTag:indexPath.row]; 


// Facebook Share Button 

UIButton *facebookButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[facebookButton setImage:[UIImage imageNamed:@"facebook"] forState:UIControlStateNormal]; 
facebookButton.frame = CGRectMake(265.0f, 0.0f, 45.0f, 42.0f); 
[cell addSubview:facebookButton]; 
[facebookButton addTarget:self 
        action:@selector(facebookShare:event:) 
     forControlEvents:UIControlEventTouchUpInside]; 
[cell.contentView addSubview:facebookButton]; 

向我建議如何編程自動佈局這些按鈕,以便所有的按鈕都應該從任何iOS設備的右側開始調整。

+0

我覺得沒有必要自動佈局的只是calcuate框架中的另一個按鈕基於設備寬度 –

+0

我添加了我的答案 –

回答

1

,如果你不使用自動佈局然後按照我的回答

第一步

創建一個宏定義設備寬度

#define WidthCalculation self.view.frame.size.width 

step2

初始設置爲您addtoFavsButton框架,它會自動調整屏幕寬度

addtoFavsButton.frame = CGRectMake(WidthCalculation - 60, 0.0f, 50.0f, 45.0f); 

此後caluculate基於addtoFavsButton

webViewButton.frame = CGRectMake(WidthCalculation - (addtoFavsButton.frame.origin.x + 50.0f +10), 0.0f, 50.0f, 50.0f); 
+0

第2步,第一行給出了準確的結果,但我試圖將按鈕稍微移動一點,所以我將'(WidthCalculation - 60,0.0f,50.0f,45.0f);'替換爲'(WidthCalculation - 45,0.0f,50.0 f,45.0f);'並且在iphone 7Plus和iphone 5s中給出了準確的結果,但是第二行沒有給出舒適的結果,它在最左邊的方向給出了按鈕,也就是'UITableViewCell'的起始點,所以我試圖改變並再次恢復數值,再次獲得一些舒適的結果,以及如何使用以下值。**繼續閱讀下一個評論,因爲低字符左** –

+0

**對不起,上述一半評論**'(addtoFavsButton.frame.origin.x - 200),0.0f,50.0f,50.0f);'這件事給予確切的位置爲我的第二個webViewButton,在iPhone 5s,當我嘗試它在iPhone 7Plus它沒有給我準確的附近位置,即跳過比預期的更多的空間.... 任何想法。? –

+0

addtoFavsButton.frame.origin.x - (。webViewButton size.width + 10),它會自動調整寬度 –

1

您正在設置這些按鈕的框架,以便不管設備的寬度如何,按鈕都會將自己置於其框架變量中定義的(x,y)座標中。因此,您必須將自動佈局約束添加到這些按鈕才能在所有設備中正確地獲得所需的設計。

使用NSLayoutConstraint對象創建您所需的自動佈局約束並將其添加到按鈕。 請參考以下鏈接:

http://www.tutorialspoint.com/ios/ios_auto_layouts.htm

+0

感謝您的回答,但它不是重點,但有幫助並提供了大量關於所需查詢的信息..去投票 –