【问题标题】:How to make button position in relation to each other?如何使按钮位置相互关联?
【发布时间】:2014-02-20 09:11:53
【问题描述】:

我正在尝试制作一个常见问题解答,因此当您单击一个按钮时,它会将文本替换为问题和答案。我正在使用此代码替换文本:

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];
    }
}

点击按钮后一切正常,但文本显示在第二个问题的顶部。

有谁知道如何让这个移动来适应答案,然后在再次点击它时备份?

谢谢

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    您应该通过将所有问题的框架从可点击问题向下移动来手动管理它,并且您应该根据分配给它们的文本增加该可点击按钮的高度。

    再次单击该已回答按钮时,会降低高度并将下行问题框架移至上行。

    使用滚动视图添加按钮,并随着按钮的点击增加和减少内容的大小。

    【讨论】:

      【解决方案2】:
      1. 使用 AutoLayout 设置您的每个问题,以便每个问题的 Top Space 约束都与前一个底线相关(当然,除了第一个,实际值可以是您想要的任何值):

      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];
        

      【讨论】:

        【解决方案3】:

        如果我正确理解了您的情况,您希望删除按钮并将文本显示在按钮所在的相同位置。

        我认为您可以将 UILabel 和 UIButton 放在同一个位置并隐藏标签。当点击按钮时,取消隐藏标签并隐藏按钮,这比更改按钮的文本更好。

        【讨论】:

        • 基本上我的问题列表显示如下:问题 1?问题2?问题3?问题4?然后,当您单击时,会在问题之间显示答案:问题 1?这是问题 1 的答案,根据答案有不同的文本行!问题2?问题3?问题4?然后,当您再次单击该按钮时,它会显示回原始列表!谢谢
        • 抱歉问题列表应该连续显示!
        猜你喜欢
        • 2021-07-09
        • 1970-01-01
        • 1970-01-01
        • 2016-05-06
        • 2011-06-09
        • 2012-01-22
        • 1970-01-01
        • 2022-10-02
        • 1970-01-01
        相关资源
        最近更新 更多