在地图应用中,当您按下左下角的跟踪按钮时,它会亮起,表示已按下该按钮.这使它的行为就像一个单选按钮,一旦你移动地图它就会取消发光.有一个简单的方法,但按钮进入按下状态?
我在它上面添加了一个UIToolbar和按钮的实例.每个按钮属于UIBarButtonItem类.
我的要求是每个按钮都有一个自定义的布局,我不想使用Apple提供的原生按钮样式.所以我在Interface Builder中有3个选项(Plain,Bordered,Done).我选择了Plain样式,并在Bar item - > Image下选择了我要添加为背景的图像.
但它对我没有用,使用普通选项使我更接近边界和完成距离更近但仍然显示白色轮廓的图像.无论如何,按钮上的图像被添加,它们看起来完全一样.使用UIButton时,这是一项非常简单的任务.我刚刚在UIButton的Attributes检查器中使用了Image选项,并选择了我想要的图像.但是在这里使用UIBarButtonitem它根本不起作用.这就是它的显示方式

谢谢泰穆尔
iphone cocoa-touch uitoolbar uibarbuttonitem uibarbuttonitemstyle
我有一个UISegmentedControl
UIBarButtonItem *flex = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]autorelease];
    bottomSegmentedControl = [[UISegmentedControl alloc] initWithItems:nil];
    [bottomSegmentedControl insertSegmentWithImage:[UIImage imageNamed:@"messages.png"] atIndex:0 animated:YES];
    [bottomSegmentedControl insertSegmentWithImage:[UIImage imageNamed:@"news.png"] atIndex:1 animated:YES];
    [bottomSegmentedControl insertSegmentWithImage:[UIImage imageNamed:@"chart.png"] atIndex:2 animated:YES];
    //bottomSegmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Messages",@"News",@"Chart", nil]];
    //bottomSegmentedControl.tintColor = [UIColor blackColor];
    [bottomSegmentedControl addTarget:self action:@selector(segmentedControlChanged:)forControlEvents:UIControlEventValueChanged];
    bottomSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    bottomSegmentedControl.frame = CGRectMake(0,0,300,30);
    bottomSegmentedControl.momentary = NO;
    [bottomSegmentedControl setSelectedSegmentIndex:0];
    UIBarButtonItem *segButton = [[UIBarButtonItem alloc] initWithCustomView:bottomSegmentedControl];
Run Code Online (Sandbox Code Playgroud)
当我单步执行程序时[bottomSegmentedControl setSelectedSegmentIndex:0];不会触发UIControlEventValueChanged事件,这是我在做某些事情的地方segmentedControlChanged:
这曾经在iOS 4.3中工作,但在iOS5中没有.我该如何解决这个问题?
我想在工具栏上有一个方形按钮.我UIBarButtonItem使用的图像宽度足以将我的按钮推入矩形.我查看了文档,但找不到最合适的大小.
看看其他答案,29.0似乎是一个常见的大小,但我想得到确认.这是我设置按钮的方式:
UIBarButtonItem *locationButtonItem = [[UIBarButtonItem alloc] 
                                   initWithImage:[UIImage imageNamed:@"location.png"]
                                   style:UIBarButtonItemStyleBordered
                                   target:self
                                   action:@selector(locationButtonTapped:)];
[locationButtonItem setWidth:29.0f];
Run Code Online (Sandbox Code Playgroud)
我应该将工具栏按钮设置为宽度为什么宽度?
这是我的代码..我无法在导航控制器中添加按钮.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    UIViewController *viewController1, *viewController2, *viewController3,*viewController4;
    viewController1 = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    viewController2 = [[DisplayAllImagesController alloc] initWithNibName:@"DisplayAllImagesController" bundle:nil];
    viewController3 = [[EmptyView alloc] initWithNibName:@"EmptyView" bundle:nil];
    viewController4 = [[ListView alloc] initWithNibName:@"ListView" bundle:nil];
    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController2, viewController1, viewController3, viewController4, nil];
    self.tabBarController.title = @"Title";
    self.navController = [[UINavigationController alloc]
                          initWithRootViewController:self.tabBarController];
    self.window.rootViewController = self.navController;
    [self.window makeKeyAndVisible];
    return YES;
}
Run Code Online (Sandbox Code Playgroud)
所以,请让我知道如何添加按钮到导航控制器...请...
选择器视图与工具栏,创建完成按钮上.单击完成按钮,它不起作用.
选择器视图向上滚动.点击完成按钮.
-(void)createPicker:(id)sender{
    pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,100,0,0)];
    [pickerView setDataSource: self];
    [pickerView setDelegate: self];
    pickerView.showsSelectionIndicator = YES;
    [pickerView setBackgroundColor:[UIColor whiteColor]];
    UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    toolBar.barStyle = UIBarStyleBlackOpaque;
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneTouched:)];
    [toolBar setItems:[NSArray arrayWithObjects:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], doneButton, nil]];
    [pickerView addSubview:toolBar];
}
Run Code Online (Sandbox Code Playgroud)
点击完成按钮关闭pickerView
-(void)doneTouched:(id)sender{
     [pickerview removeFromSuperview];
 }
Run Code Online (Sandbox Code Playgroud)
我不知道我在这里做错了什么.任何人都可以建议我如何调用uipickerview工具栏按钮上添加的完成按钮方法.
点击完成选择器视图向上滚动,而不是调用doneTouched方法:
@All先谢谢.
我有一个UIBarButton saveBarButton屏幕图像保存到相机胶卷。当用户按下saveBarButton图像被保存并且UIBarButton saveBarButton改变UIImage doneIcon为 1.2 秒时,向用户表明图像已被保存。当saveBarButton显示doneIconI disable 时saveBarButton。但是,当我这样做时,它saveBarButton会变灰。
我的问题是:如何阻止UIBarButton禁用时变灰?
我的代码:
//create UIBarButton saveBarButton
override func viewDidLoad() {    
    let saveBarButton = UIBarButtonItem(image: UIImage(named: "saveIcon"), style: .Plain, target: self, action: "save:")
    saveBarButton.tintColor = colorGreyDark
}
//save function called when press saveBarButton
func save(sender: UIBarButtonItem) {
    //save image
    deselectShape()
    let window: UIWindow! = UIApplication.sharedApplication().keyWindow
    let windowImage = capture(window)
    UIImageWriteToSavedPhotosAlbum(windowImage
        , nil, nil, nil)
    //Change saveBarButton …Run Code Online (Sandbox Code Playgroud) 我正在尝试以编程方式覆盖带有静态图像的导航项的 rightButton 以实现此目的:
以下代码没有给出任何错误,但导航栏中没有显示任何内容。
import UIKit
import Foundation
class UICipNavigationController: UINavigationController {
override func viewDidLoad() {
    super.viewDidLoad()
    // Add BPT LOGO as UIBarButton
    let logoBPT = UIImage(named: "Logo BPT")?.withRenderingMode(.alwaysOriginal)
    let logoBPTBarButton = UIBarButtonItem(image: logoBPT, style: .plain, target: nil, action: nil)
    self.navigationItem.rightBarButtonItem = logoBPTBarButton
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}
Run Code Online (Sandbox Code Playgroud)
.
import UIKit
class UICipNavigationBar: UINavigationBar {
override init(frame: CGRect){
    super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)! …Run Code Online (Sandbox Code Playgroud) 我写了一个扩展,UIButton可以将 a 转换UIButton为UIBarButtonItem.
我尝试使用传递按钮init(customView来实现最简单的解决方案,如下所示:UIBarButtonItem
lazy var myButton: UIButton = { 
    let button = UIButton(type: .custom)
    // setting up button here...
    return button
}()
let barButtonItem = UIBarButtonItem(customView: myButton)
Run Code Online (Sandbox Code Playgroud)
但我遇到了一些限制问题。其实我不想只放一个按钮,而是想在导航栏中放三个项目。所以我尝试将三个按钮放入 aUIStackView然后设置,这在 iOS 10 上navigationItem.rightBarButtonItem = UIBarButtonItem(customView: stackView)就像一个魅力。但在iOS 9上,按钮的位置不起作用。
无论如何,我求助于使用UIBarButtonItem而不是UIButtons,但我不想UIBarButton为 iOS 9 创建 s,而是UIButton为 iOS 10 创建 s。所以我编写了一个UIBarButtonItem从 s 创建 s的扩展UIButton。这启用了此代码。
我正在尝试以编程方式设置左键按钮项目的大小,但我不能.
这是我的代码:
        let backButton = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
        backButton.setBackgroundImage(UIImage(named: "hipster_pelo2.png"), for: .normal)
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backButton)
Run Code Online (Sandbox Code Playgroud)
但这是iphone X与Xcode 9和swift 3的结果.在图像中,您可以看到标题应用程序将其移动到右侧,因为按钮大小:
谁知道问题将是图像大小?
uibarbuttonitem ×10
ios ×5
iphone ×5
objective-c ×4
swift ×3
swift3 ×3
cocoa-touch ×2
dimensions ×1
ios5 ×1
toolbar ×1
uikit ×1
uipickerview ×1
uitoolbar ×1
xcode ×1