设置uibutton框架不起作用

时间:2015-10-15 16:33:47

标签: objective-c uibutton frame

我在词典中有值,它会构成一个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>>"
)

1 个答案:

答案 0 :(得分:1)

让我们来看看代码......

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

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

您创建一个新词典,并立即使用tags数组中的项目覆盖该引用。

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

其余代码没问题。

无论如何,你的问题是引用错误的字典。你没注意到标题也错了吗?