Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 使用customView时UINavigationBarButton不工作_Objective C_Uiview_Uinavigationbar_Custom View_Programmatically - Fatal编程技术网

Objective c 使用customView时UINavigationBarButton不工作

Objective c 使用customView时UINavigationBarButton不工作,objective-c,uiview,uinavigationbar,custom-view,programmatically,Objective C,Uiview,Uinavigationbar,Custom View,Programmatically,我想做一个自定义UIBarButtonItem,它包含图像和文本,还可以自定义(减少)UIButton点击区域。为了以编程方式实现这一点,我使用以下代码段。但当作为子视图添加到UIView中时,UIButton将消失。有人能告诉我怎么了吗? 为了减少条形按钮的点击面积,我将在自定义UIView中嵌入自定义UIButton //Set Navigation Bar UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CG

我想做一个自定义UIBarButtonItem,它包含图像和文本,还可以自定义(减少)UIButton点击区域。为了以编程方式实现这一点,我使用以下代码段。但当作为子视图添加到UIView中时,UIButton将消失。有人能告诉我怎么了吗? 为了减少条形按钮的点击面积,我将在自定义UIView中嵌入自定义UIButton

//Set Navigation Bar
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];

//Set title if needed
UINavigationItem * navTitle = [[UINavigationItem alloc] init];
navTitle.title = @"";
UIView *customBackView = [[UIView alloc] initWithFrame:CGRectMake(220,0,50,40)];
customBackView.backgroundColor = [UIColor clearColor];

//Here you create info button and customize it
UIButton * tempButton = [UIButton buttonWithType:UIButtonTypeSystem];
tempButton.frame=CGRectMake(240,2,40,40);
[tempButton setImage:[UIImage imageNamed:@"offline_bk.png"]
         forState:UIControlStateNormal];

//Add selector to info button
[tempButton addTarget:self action:@selector(onTapDone:) forControlEvents:UIControlEventTouchUpInside];
[customBackView addSubview:tempButton];
[customBackView bringSubviewToFront:tempButton];
UIBarButtonItem * infoButton = [[UIBarButtonItem alloc] initWithCustomView:customBackView];

//In this case your button will be on the right side
navTitle.rightBarButtonItem = infoButton;

//Add NavigationBar on main view
navBar.items = @[navTitle];
[self.view addSubview:navBar];

因为您的tempbutton超出了自定义后视图的范围。您将y原点值设置为2,将x原点值设置为240,这将导致按钮超出自定义后视图的范围

请尝试以下代码:

 UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];

    //Set title if needed
    UINavigationItem * navTitle = [[UINavigationItem alloc] init];
    navTitle.title = @"";
    UIView *customBackView = [[UIView alloc] initWithFrame:CGRectMake(0,0,50,50)];
    customBackView.backgroundColor = [UIColor clearColor];

    //Here you create info button and customize it
    UIButton * tempButton = [UIButton buttonWithType:UIButtonTypeSystem];
    tempButton.frame=CGRectMake(4,2,40,40);
    [tempButton setImage:[UIImage imageNamed:@"offline_bk.png"]
                forState:UIControlStateNormal];

    //Add selector to info button
    [tempButton addTarget:self action:@selector(onTapDone:) forControlEvents:UIControlEventTouchUpInside];
    [customBackView addSubview:tempButton];
    [customBackView bringSubviewToFront:tempButton];
    UIBarButtonItem * infoButton = [[UIBarButtonItem alloc] initWithCustomView:customBackView];

    //In this case your button will be on the right side
    navTitle.rightBarButtonItem = infoButton;

    //Add NavigationBar on main view
    navBar.items = @[navTitle];
    [self.view addSubview:navBar];
    [self.view bringSubviewToFront:navBar];

感谢您的回复,但UIButton图像不可见,我们无法确定按钮是否存在于右角。您可以。您可以使用视图调试器遍历视图层次结构以确定按钮是否存在。图像大小可能有问题。请根据按钮大小尝试图像文件。我已尝试使用虚拟图像,但它正在工作。请确保您使用的图像分辨率正确,并且添加正确要捆绑项目,是否需要在资产中添加图像?我正在添加32*32的图像,但它不可见。没有。但对于视网膜显示,您需要2倍分辨率的图像,这意味着您的按钮需要80*80的图像,并确保在您将图像添加到捆绑包时,它应该列在“目标->构建阶段->复制捆绑包资源”下