【问题标题】:Adjusting button size in iPhone在 iPhone 中调整按钮大小
【发布时间】:2010-12-29 00:19:58
【问题描述】:

我将一个圆角矩形按钮放入表格视图页脚。

但是无论我如何更改 BUTTON_WIDTH,按钮都会向左和向右拉伸。但它的高度是可调的。

有什么问题?下面是我使用的代码。


#define BUTTON_WIDTH 80
#define BUTTON_HEIGHT 45

- (void)viewDidLoad {
    CGRect frame = CGRectMake(0, 0, BUTTON_WIDTH, BUTTON_HEIGHT);

// btnSeeResult is decleared in header file
    btnSeeResult = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btnSeeResult setTitle:@"Result" forState:UIControlStateNormal];
    [btnSeeResult addTarget:self action:@selector(seeResult) forControlEvents:UIControlEventTouchUpInside];
    btnSeeResult.frame = frame;

    self.tableView.tableFooterView = btnSeeResult;
}

【问题讨论】:

    标签: iphone uibutton


    【解决方案1】:

    这是因为您将按钮设置为tableView.tableFooterView,因此视图的宽度将始终设置为tableView 的宽度。

    尝试使用黑色的UIView 作为页脚视图,然后向其添加按钮,如下所示:

    #define BUTTON_WIDTH 80
    #define BUTTON_HEIGHT 45
    
    - (void)viewDidLoad {
        CGRect frame = CGRectMake(0, 0, BUTTON_WIDTH, BUTTON_HEIGHT);
    
        // btnSeeResult is decleared in header file
        UIButton *btnSeeResult = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [btnSeeResult setTitle:@"Result" forState:UIControlStateNormal];
        [btnSeeResult addTarget:self action:@selector(seeResult) forControlEvents:UIControlEventTouchUpInside];
        btnSeeResult.frame = frame;
    
        // the width (320.0) actually doesn't matter here
        UIView *footerView = [[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 80.0, 320.0)] autorelease];
        footerView.backgroundColor = [UIColor clearColor];
        [footerView addSubview:btnSeeResult];
    
        self.tableView.tableFooterView = footerView;
    }
    

    【讨论】:

    • 好的。这样可行。但还有几个问题。 1. 使用 UIButton 作为页脚和使用另一个 UIView 有什么区别?按钮不是从 UIView 派生的吗?我认为结果应该是一样的。 2、我直接使用UIButton时,可以改变它的高度。只是宽度不能改变。这是为什么呢?
    【解决方案2】:

    @SeniorLee 根据您在评论中留下的问题-

    当您使用按钮作为页脚视图时它不起作用的原因是页脚视图必须是表格视图的宽度。这就是为什么当您的按钮是您的页脚视图时,它遵循的规则是它必须是表格的宽度。

    现在,当您使用常规的旧 UIView 时,它会代替 footerview,并且它的宽度与表格的宽度相同。然后,您添加到该视图中的任何内容都可以是您想要的任何内容。这有意义吗?

    之所以可以改变高度是因为footerview的高度没有和宽度一样的强制规定,所以你可以随意改变footer的高度,而不是宽度。

    【讨论】:

    • 我认为 UIView 和 UIButton 都没有被拉伸,但实际上 UIView 被拉伸但它没有被绘制,并且 UIButton 可以是任何大小。这就说得通了。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2017-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-18
    • 2021-09-20
    相关资源
    最近更新 更多