我使用以下代码将UITabButtonItem添加到我的视图控制器上.任何人都可以告诉我它是否可以将图像与它联系起来?
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(favouriteButtonClicked:)];
viewController2.navigationItem.rightBarButtonItem = button;
[button release];
[self.navigationController pushViewController:viewController2 animated:YES];
Run Code Online (Sandbox Code Playgroud)
我在其他地方使用以下代码,但在上面的代码我将viewcontroller推到我当前的视图,这段代码不起作用.我不得不使用上面的代码.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"unselected.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(favouriteButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(280, 25, 30, 30)];
viewController2.navigationController.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
[self.navigationController pushViewController:viewController2 animated:YES];
Run Code Online (Sandbox Code Playgroud)
我不知道有什么问题但是当我用initWithBarButtonSystemItem替换这个自定义按钮代码时,它工作得很好!!!
最后,我整理出这个问题,并张贴在这里工作的代码,但仍,如果我有在baseviewcontroller一个按钮,我不明白,我添加它,就象下面,这是行不通的!我必须在viewwillappear中使用我接受的答案中给出的代码!
[favButton addTarget:self action:@selector(favouriteButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
Run Code Online (Sandbox Code Playgroud)
以下代码仍然无法正常工作!不知道发生了什么.
MPMoviePlayerViewController *videoController = [[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:filePath]] autorelease];
UIButton* favouritebutton = [UIButton buttonWithType:UIButtonTypeCustom];
[favouritebutton setBackgroundImage:[UIImage imageNamed:@"unselected.png"] forState:UIControlStateNormal];
[favouritebutton addTarget:self action:@selector(favouriteButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[favouritebutton …Run Code Online (Sandbox Code Playgroud) 我在我的UIToolbar中有一个按钮,我已经分配了一个图像,但我希望图像能够自动缩小(调整应用程序外部图像的尺寸会降低一些质量).
我在这里尝试了解决方案,它创建了一个自定义的imageView,然后将其分配给按钮.但是,图像似乎没有出现.这是我的代码:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"info.png"]];
imageView.frame = CGRectMake(0, 0, 35, 35);
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.userInteractionEnabled = YES;
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:imageView];
self.tutorial_lbl = barButtonItem;
Run Code Online (Sandbox Code Playgroud)
请注意,如果我注释掉最后两行并使用下面的行代替,则会出现图像但是它会丢失按钮的操作.
[self.tutorial_lbl setCustomView:imageView];
Run Code Online (Sandbox Code Playgroud) 如何在代码中调整UIBarButtonItem的大小?
如何将a的宽度更改UIBarButtonItem为29px?属性宽度不起作用,我不想创建UIButton并使用,initWithCustomView因为我希望方形背景调整为导航栏颜色.我正在使用initWithImage初始化UIBarButtonItem.
这是我的按钮,当前宽度为37px.我想设置为29px.

我喜欢将我的视图创建为独立的Xib文件,然后实例化它们并添加为子视图.
因此,在使用a时UINavigationBar,我希望能够做同样的事情,首先创建我的自定义视图 - 从Xib - 然后将其作为自定义视图添加到UIBarButtonItem:
UIBarButtonItem *anItem = [[UIBarButtonItem alloc] initWithCustomView:_myCustomView];
Run Code Online (Sandbox Code Playgroud)
然后添加到导航栏:
self.navigationBar.topItem.rightBarButtonItems = @[ anItem, anotherItem ];
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.
现在,_myCustomView使用自动布局(AL),我认为这没有问题.不是这样的.我已经尝试了一切.没有任何效果.我甚至尝试将自定义视图添加为具有导航栏的控制器的子视图.认为作为视图层次结构中的兄弟姐妹,AL会将其视为常规视图UINavigationBar.
那也行不通.控制器updateViewConstraints被调用但从未应用过.视图的初始框架保持不变CGRectZero.好像AL看到视图位于a之上UINavigationBar,即使是兄弟,也决定它不需要布局.
当然,我已经尝试了bringSubviewToFront,translatesAutoresizingMaskIntoConstraintstranslatesAutoresizingMaskIntoConstraints等等.后者给了可爱的:
由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'执行-layoutSubviews后仍需要自动布局.MyNavigationBar的-layoutSubviews实现需要调用super.
所以,问题是,是否有人从Xib加载了AL的自定义视图,并成功将其设置为customViewon UIBarButtonItem?on ?如果是这样,怎么样?
ios ×3
autolayout ×1
cocoa-touch ×1
image ×1
iphone ×1
objective-c ×1
uiimage ×1
uitoolbar ×1
width ×1