设置 uibutton 框架不起作用

Setting uibutton frame not working

我在字典中有值可以构成 uibutton 的框架

tags =         (
                    {
            height = "15.513672";
            text = jeans;
            width = "39.808105";
            x = "225.500000";
            y = "265.000000";
        },
                    {
            height = "15.513672";
            text = jacket;
            width = "44.080078";
            x = "190.000000";
            y = "156.500000";
        }
    );

我用字典中的值设置了 uibuttons 的框架

for (int i = 0; i<[tags count]; i++) {
    NSLog(@"adding tags");

    NSMutableDictionary *propertiesdict = [[NSMutableDictionary alloc] init];
    propertiesdict = [[tags objectAtIndex:i] mutableCopy];

    float x = [[dict objectForKey:@"x"] floatValue];
    float y = [[dict objectForKey:@"y"] floatValue];
    float width = [[dict objectForKey:@"width"] floatValue];
    float height = [[dict objectForKey:@"height"] floatValue];

    NSString *title = [dict objectForKey:@"text"];

    UIButton *button = [[UIButton alloc] init];
    [button setFrame:CGRectMake(x, y, width, height)];
    [button setTitle:title forState:UIControlStateNormal];
    button.layer.cornerRadius = 2.5;
    //button.alpha = 0.9;
    [button.titleLabel setFont:[UIFont systemFontOfSize:13]];
    [button setBackgroundColor:[UIColor viewFlipsideBackgroundColor]];
    button.tag = i;
    [button addTarget:self action:@selector(tappedOnTag:) forControlEvents:UIControlEventTouchUpInside];

    [tagsView addSubview:button];
}

将按钮添加到子视图后,NSLog 显示每个按钮都有一个框架 (0,0,0,0),这与我在 for 循环中设置框架时不同。

2015-10-15 12:25:17.787 kyss[1018:316548] tapped on tags view. Number of subviews = 2. Subviews: (
    "<UIButton: 0x15f732330; frame = (0 0; 0 0); opaque = NO; layer = <CALayer: 0x15f72eb60>>",
    "<UIButton: 0x15f6d10e0; frame = (0 0; 0 0); opaque = NO; tag = 1; layer = <CALayer: 0x15f6b3790>>"
)

让我们看一下代码...

for (int i = 0; i<[tags count]; i++) {
    NSLog(@"adding tags");

    NSMutableDictionary *propertiesdict = [[NSMutableDictionary alloc] init];
    propertiesdict = [[tags objectAtIndex:i] mutableCopy];

您创建了一个新词典并立即用标签数组中的项目覆盖了引用。

    float x = [[dict objectForKey:@"x"] floatValue];
    float y = [[dict objectForKey:@"y"] floatValue];
    float width = [[dict objectForKey:@"width"] floatValue];
    float height = [[dict objectForKey:@"height"] floatValue];

您引用了 dict,它未在您发布的代码中定义。可以安全地假设它与 propertiesdict 不同,因此您将获得 nil 引用,因此每个赋值的值为 0。

    NSString *title = [dict objectForKey:@"text"];

再次使用 dict

    UIButton *button = [[UIButton alloc] init];
    [button setFrame:CGRectMake(x, y, width, height)];
    [button setTitle:title forState:UIControlStateNormal];
    button.layer.cornerRadius = 2.5;
    //button.alpha = 0.9;
    [button.titleLabel setFont:[UIFont systemFontOfSize:13]];
    [button setBackgroundColor:[UIColor viewFlipsideBackgroundColor]];
    button.tag = i;
    [button addTarget:self action:@selector(tappedOnTag:) forControlEvents:UIControlEventTouchUpInside];

    [tagsView addSubview:button];
}

剩下的代码没问题。

无论如何,您的问题是引用了错误的词典。没发现标题也错了吗?