我想弄清楚如何在UIActionSheet
的按钮右侧添加复选标记。
我想模仿AirPlay动作表中的复选标记。据我所知,定制这些按钮意味着要访问苹果的私有API,这将使你面临被App Store拒绝的风险。
是否有一种安全的方法来添加这个复选标记?
这个方法怎么样?
在按钮内放置一个带有"tick"的标签并切换它。下面的代码就是这样做的。切换是通过KVO完成的,但可以用更简单的方式完成…
按钮通过self.theButton
在IB连接
- (void)viewDidLoad
{
[super viewDidLoad];
// Create a 'tick' label
CGRect rect;
rect.size = CGSizeMake(17, 21);
rect.origin.x = self.theButton.frame.size.width-17-10;
rect.origin.y = (self.theButton.frame.size.height-21)/2;
UILabel *label = [[UILabel alloc] initWithFrame:rect];
label.text = [NSString stringWithCString:"342234223" encoding:NSUTF8StringEncoding];
label.backgroundColor = [UIColor clearColor];
label.hidden = YES;
// Put label inside the button
[self.theButton addSubview:label];
// Connect a observer on 'highlighted' (eq pressed basically)
// could use another method to track 'selected'
[self.theButton addObserver:self
forKeyPath:@"highlighted"
options:0
context:(__bridge void*)label];
}
// Toggle the 'tick' label
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(UIButton*)button
change:(NSDictionary *)change
context:(UILabel *)label
{
if (button.highlighted)
label.hidden = !label.hidden;
}
// Don't forget to cleanup
- (void)dealloc
{
[self.theButton removeObserver:self forKeyPath:@"highlighted"];
}
NSString* strUrl=[MLControl shared].currentServerUrl;
for( MLServerUrl *title in [MLControl shared].arrServerUrl) {
NSString* strShow=title.name;
if ([strUrl isEqualToString: title.url]) {
strShow=[NSString stringWithFormat:@"√ %@",strShow];
}else{
strShow=[NSString stringWithFormat:@" %@",strShow];
}
[chooseImageSheet addButtonWithTitle:strShow];
}
// [[[chooseImageSheet valueForKey:@"_buttons"] objectAtIndex:0] setImage:[UIImage imageNamed:@"ic_check_black_18dp.png"] forState:UIControlStateNormal];
chooseImageSheet.actionSheetStyle = UIActionSheetStyleDefault;
[chooseImageSheet showFromRect:btnRc inView:sender animated:YES];