Rog*_*uri 4 iphone xcode customization objective-c ios
我正在努力让左按钮正常工作并模仿后退按钮.
我创建按钮的代码:
UIBarButtonItem *backButton = [self customBarButton:@"back_button" imageHiglighted:@"settings_button_highlighted" x:20 y:0 widthDivider:2.6 heightDivider:2.6];
backButton.target = self;
backButton.action = @selector(buttonPressed:);
self.navigationItem.leftBarButtonItem = backButton;
Run Code Online (Sandbox Code Playgroud)
这里调用创建自定义按钮的方法:
- (UIBarButtonItem *)customBarButton:(NSString *)imageDefault imageHiglighted:(NSString *)imageHighlighted x:(float)x y:(float)y widthDivider:(float)widthDivider heightDivider:(float)heightDivider {
UIImage *customImageDefault = [UIImage imageNamed:imageDefault];
UIImage *customImageHighlighted = [UIImage imageNamed:imageHighlighted];
CGRect frameCustomButton = CGRectMake(x, y, customImageDefault.size.width/widthDivider, customImageDefault.size.height/heightDivider);
UIButton *customButton = [[UIButton alloc] initWithFrame:frameCustomButton];
[customButton setBackgroundImage:customImageDefault forState:UIControlStateNormal];
[customButton setBackgroundImage:customImageHighlighted forState:UIControlStateHighlighted];
UIBarButtonItem *barCustomButton =[[UIBarButtonItem alloc] initWithCustomView:customButton];
return barCustomButton;
}
Run Code Online (Sandbox Code Playgroud)
行动:
-(void)buttonPressed:(id) sender{
NSLog(@"Entered");
SearchViewController *ViewController = [[SearchViewController alloc] init];
[self.navigationController pushViewController:ViewController animated:YES];
}
Run Code Online (Sandbox Code Playgroud)
所以我能用一个简单的UIButton但不能用UIButtonBarItem来制作它,我真的不知道它是怎么回事.
如果你能帮助我,我会非常感激.
谢谢.
做到这一点添加selector到自定义的button,因为它是view的barBUTTOM:
[customButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
Run Code Online (Sandbox Code Playgroud)
编辑:请注意:该target和action的UIBarButtonItem申请custom views.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"goback.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(//HERE ! i don't know put what thing !) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 32, 32)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11674 次 |
| 最近记录: |