我遇到了关于 UIScrollView 的问题。我正在制作一个继承 UIView 的自定义视图。该视图有一个 UIScrollView,上面有很多应该左右滚动的按钮。UIScrollView 和按钮可以正常显示。但我无法滚动按钮。有人可以给我一些建议吗?非常感谢!
MZMPhotoCalenderSwitcher.h
#import <UIKit/UIKit.h>
@interface MZMPhotoCalenderSwitcher : UIView <UIScrollViewDelegate>
@property (strong, nonatomic) UIScrollView *topSwitcher;
@end
MZMPhotoCalenderSwitcher.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.topSwitcher = [[UIScrollView alloc] initWithFrame:CGRectMake(0, LABEL_HEIGHT + VIEW_Y, self.view.bounds.size.width, TOP_SWITCHER_HEIGHT)];
self.topSwitcher.backgroundColor = [UIColor greenColor];
self.topSwitcher.pagingEnabled = YES;
self.topSwitcher.showsHorizontalScrollIndicator = NO;
self.topSwitcher.showsVerticalScrollIndicator = NO;
[self add:3 ButtonsOnView:self.topSwitcher withButtonWidth:44.8f andHeight:20.0f];
}
- (void)add:(int)num ButtonsOnView:(UIScrollView *)view withButtonWidth:(CGFloat)width andHeight:(CGFloat)height
{
CGFloat totalTopSwitcherWidth = num * width;
[view setContentSize:CGSizeMake(totalTopSwitcherWidth, view.bounds.size.height)];
CGFloat xOffset = 0.0f;
for (int i=1; i<=num; i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(xOffset, 0, width, height)];
xOffset += width;
[button setTitle:[NSString stringWithFormat:@"%d", i] forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:10];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
[button setTag:i];
[button addTarget:self action:@selector(buttonEvent) forControlEvents:UIControlEventTouchUpInside];
if (i % 2 == 0)
[button setBackgroundColor:[UIColor yellowColor]];
else
[button setBackgroundColor:[UIColor redColor]];
[view addSubview:button];
}
}