以编程方式添加自定义按钮互相搞乱

时间:2013-06-20 05:02:04

标签: ios uibutton position

我有一个虚空我正在调用当我滑过时显示隐藏在一个单元格上的视图,当它们被设置为图标时,定位工作得很好,但是将它们更改为自定义按钮,所以我可以使用文本而不是图标,把事搞砸了。

编辑:同样,如果我触摸按钮应该在哪里,它就好像真的存在,只是“看起来”搞砸了。例如,如果在示例中它的错误(图像)如何在左边的decline1,如果我点击它,它接受请求,但如果我点击中间的空白区域,没有。

这是编码

- (void)bottomDrawerWillAppear {
    UIImageView *drawerBGImg = [[UIImageView alloc]  initWithFrame:CGRectMake(0,0,320,75)];
    NSString *friendAvatar = [NSString stringWithFormat:@"%@%@%@", @"http://www.thatonewebsite.com/images/users/", [MyClass friendID], @".jpg"];
    [drawerBGImg setImageWithURL:[NSURL URLWithString:friendAvatar]];
    self.bottomDrawer.clipsToBounds = YES;
    drawerBGImg.contentMode = UIViewContentModeScaleAspectFill;
    [self.bottomDrawer addSubview:drawerBGImg];

    UIImageView *drawerBG = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,75)];
    drawerBG.image = [UIImage imageNamed:@"drawerBG.png"];
    [self.bottomDrawer addSubview:drawerBG];
    NSLog(@"%@", [MyClass friendID]);

    UIButton *inviteToLocationBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    inviteToLocationBtn.frame=CGRectMake(15.0, 15.0, 80.0, 50.0);
    [inviteToLocationBtn setTitle:@"accept" forState:UIControlStateNormal];
    inviteToLocationBtn.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size: 22.0f];
    inviteToLocationBtn.titleLabel.textColor = [UIColor whiteColor];
    [inviteToLocationBtn addTarget:self action:@selector(callAccept) forControlEvents:UIControlEventTouchUpInside];
    [self.bottomDrawer addSubview:inviteToLocationBtn];

    UIButton *deleteBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    deleteBtn.frame=CGRectMake(130.0, 15.0, 80.0, 50.0);
    [inviteToLocationBtn setTitle:@"decline1" forState:UIControlStateNormal];
    inviteToLocationBtn.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size: 22.0f];
    inviteToLocationBtn.titleLabel.textColor = [UIColor whiteColor];
    [deleteBtn addTarget:self action:@selector(callDeny) forControlEvents:UIControlEventTouchUpInside];
    [self.bottomDrawer addSubview:deleteBtn];

    UIButton *messageBtn=[UIButton buttonWithType:UIBarButtonSystemItemCompose];
    messageBtn.frame=CGRectMake(245.0, 15.0, 50.0, 50.0);
    UIImage *messageImage = [UIImage imageNamed:@"messageIcon.png"];
    [messageBtn setImage:messageImage forState:UIControlStateNormal];
    [messageBtn addTarget:self action:@selector(callChat) forControlEvents:UIControlEventTouchUpInside];
    [self.bottomDrawer addSubview:messageBtn];

}

在你看一下之前,我为大图片道歉 这就是它的样子 enter image description here

这是它应该看的样子,中间图像,邀请。

enter image description here

1 个答案:

答案 0 :(得分:3)

声明deleteBtn后,将标题设置为@" decline1"但是请调用inviteToLocationBtn。 [inviteToLocationBtn setTitle:@" decline1" forState:UIControlStateNormal]应该引用deleteBtn。

以上这一行:

UIButton *deleteBtn=[UIButton buttonWithType:UIButtonTypeCustom];
deleteBtn.frame=CGRectMake(130.0, 15.0, 80.0, 50.0);
[inviteToLocationBtn setTitle:@"decline1" forState:UIControlStateNormal];
inviteToLocationBtn.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size: 22.0f];
inviteToLocationBtn.titleLabel.textColor = [UIColor whiteColor];
[deleteBtn addTarget:self action:@selector(callDeny) forControlEvents:UIControlEventTouchUpInside];
[self.bottomDrawer addSubview:deleteBtn];

应该是:

UIButton *deleteBtn=[UIButton buttonWithType:UIButtonTypeCustom];
deleteBtn.frame=CGRectMake(130.0, 15.0, 80.0, 50.0);
[deleteBtn setTitle:@"decline1" forState:UIControlStateNormal];
deleteBtn.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size: 22.0f];
deleteBtn.titleLabel.textColor = [UIColor whiteColor];
[deleteBtn addTarget:self action:@selector(callDeny) forControlEvents:UIControlEventTouchUpInside];
[self.bottomDrawer addSubview:deleteBtn];