【问题标题】:UIButton style when added to navigationItem.titleView添加到 navigationItem.titleView 时的 UIButton 样式
【发布时间】:2012-02-15 19:12:59
【问题描述】:

我需要在 UINavigationItem titleView 上添加一个按钮(实际上是在 UINavigation Bar 的中心。

我使用以下代码来完成此操作:

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 70, 30);
[btn setSelected:YES];
[btn setTitle:@"list" forState:UIControlStateNormal];

我做了以下事情:

我需要将相同样式的“完成”按钮(即UIBarButtonItem)赋予“列表”按钮?

【问题讨论】:

    标签: iphone uibutton uinavigationitem titleview uibuttonbaritem


    【解决方案1】:

    你可以使用 UISegmentedControl:

    UISegmentedControl *mySegmentedControl = [[UISegmentedControl alloc]
                    initWithItems:@"Example"]];
    mySegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    [mySegmentedControl addTarget:self action:@selector(myMethod)
           forControlEvents:UIControlEventValueChanged];
    
    self.navigationItem.titleView = mySegmentedControl;
    

    【讨论】:

      【解决方案2】:

      UIBarButtonItems 只能用作 UIToolbar 中的项目(它们实际上不是 UIView 的实例),因此在您的 titleView 中直接使用 UIBarButtonItem 的唯一方法是将 UIToolbar 的实例设置为您的 titleView 并添加您的barButtonItem 到该工具栏。但是,我认为这不会奏效。

      我认为您需要做的是找到一种使用普通旧 UIButton 来模仿 UIBarButtonItem 样式的方法。您可以使用UIKit artwork extractor 获取用于创建 UIBarButtonItems 的实际图像文件。然后,只需使用该背景图像创建一个自定义 UIButton,您就可以开始了。

      【讨论】:

        【解决方案3】:

        我使用了 andrej 的基于 UISegmentedControl 的方法,并对其进行了扩展,使其表现得更像一个普通的 UIButton:

        @interface SPWKBarButton : UISegmentedControl
        - (id)initWithTitle:(NSString *)title;
        - (void)setTitle:(NSString *)title;
        @end
        
        @implementation SPWKBarButton 
        
        - (id)initWithTitle:(NSString *)title
        {
            self = [super initWithItems:@[title]];
        
            if (self) {
                self.segmentedControlStyle = UISegmentedControlStyleBar;
        
                NSDictionary *attributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor]
                                                                       forKey:UITextAttributeTextColor];
        
                [self setTitleTextAttributes:attributes
                                    forState:UIControlStateNormal];
            }
        
            return self;
        }
        
        - (void)setTitle:(NSString *)title
        {
            [self setTitle:title forSegmentAtIndex:0];
        }
        
        - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
        {
            [super touchesMoved:touches withEvent:event];
        
            if (CGRectContainsPoint(self.bounds, [[touches anyObject] locationInView:self])) {
                self.selectedSegmentIndex = 0;
            } else {
                self.selectedSegmentIndex = UISegmentedControlNoSegment;
            }
        }
        
        - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
        {
            [super touchesEnded:touches withEvent:event];
        
            if (CGRectContainsPoint(self.bounds, [[touches anyObject] locationInView:self])) {
                [self sendActionsForControlEvents:UIControlEventTouchUpInside];
            }
        
            self.selectedSegmentIndex = UISegmentedControlNoSegment;
        }
        
        @end
        

        对于基本用法,您可以将其用作 UIButton 插件替换:

        _button = [[SPWKBarButton alloc] initWithTitle:titles[0]];
        
        [_button addTarget:self
                    action:@selector(doSomething:)
          forControlEvents:UIControlEventTouchUpInside];
        
        [self.navigationItem setTitleView:_button];
        

        仅当触摸发生在分段控件的边界内时才会触发该事件。享受吧。

        【讨论】:

          【解决方案4】:

          是的,您也应该为按钮“列表”使用 UIBarButtonItem

          使用自定义标识符和标题属性。

          您可以使用 UIBarButtonItem 标识符“灵活空间”来在中心显示您的按钮“列表”。

          【讨论】:

            猜你喜欢
            • 2011-06-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-12-25
            • 2010-12-27
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多