2014-02-20 32 views
-2

我想提出一個FAQ,所以當你點擊一個按鈕時,它會用問題和答案替換文本。我正在使用此代碼來替換文本:如何使按鈕相對於彼此的位置?

ViewContoller.m 

@interface ViewController : UIViewController 
@property (strong, nonatomic) IBOutlet UIButton *buttonTest; 
- (IBAction)ButtonTest:(id)sender; 

@end 

ViewController.h 

- (IBAction)ButtonTest:(id)sender 
{ 
    if ([_buttonTest.titleLabel.text isEqualToString:@"This is the Question No1?"]) 
    { 
     [sender setTitle:@"This is the Question No1? This is question No1 answer that enter code herehas a fair bit of content" forState:UIControlStateNormal]; 
    } 
    else 
    { 
     [sender setTitle:@"What age is This is the Question No1?" forState:UIControlStateNormal]; 
    } 
} 

當單擊按鈕時,它一切正常,但文本顯示在第二個問題的頂部。

有誰知道如何讓這個下移來適應答案,但當它再次點擊時備份?

感謝

回答

-1

如果我理解您的方案正確,您要刪除的按鈕,並顯示在同一個地方,你的按鈕是你的文字。

我認爲你可以把你的UILabel和UIButton放在同一個地方,並保持你的標籤hidden.and當按鈕被輕敲取消隱藏你的標籤和隱藏按鈕,這將比改變你的按鈕的文本更好。

+0

我基本上我的列表中顯示的是這樣的: 問題1? 問題2? 問題3? 問題4? 然後當你點擊是顯示問題之間的答案: 問題1? 這是問題1的答案,根據答案的不同文本行! 問題2? 問題3? 問題4? 然後當你再次點擊該按鈕時,它會顯示回原來的列表! 謝謝 – user3255316

+0

對不起,問題列表應該連續顯示! – user3255316

0

您應該通過從可點擊問題中移除所有問題的框架來手動管理它,並且應該根據您指定的文本增加可點擊按鈕的高度。

再次點擊答案按鈕時,降低高度,並將下面的問題框架移到正面。

使用滾動視圖添加按鈕並增加和減少內容大小以及點擊按鈕。

0
  1. 設置每個使用自動版式您的問題,讓每個人的頂層空間約束與之前的底線(除了過程中的第一個,實際值可以是任何你願意的話):單擊該按鈕時

    enter image description here

  2. 編程調整你的問題的高度。
    可選:如果您需要測量問題+答案的所需高度,this question可能會有所幫助。基本上就是你需要做的是使用NSString+Geometrics Category像這樣:問題

    NSDictionary *attributes 
        = [NSDictionary dictionaryWithObjectsAndKeys: 
        [questionField font],NSFontAttributeName,nil]; 
    NSAttributedString *attributedString 
        = [[NSAttributedString alloc] initWithString: 
        [questionField title] attributes:attributes]; 
    // Here below the height of the string 
    CGFloat height = [attributedString heightForWidth: 
           [questionField frame].size.width];