Pre*_*ung 2 iphone objective-c uiscrollview uiview ios
我遇到了关于UIScrollView的问题.我正在制作一个继承UIView的自定义视图.该视图有一个UIScrollView,其中有许多按钮可以向左和向右滚动.UIScrollView和按钮可以正常显示.但我无法滚动按钮.有人可以给我一些建议吗?非常感谢!
MZMPhotoCalenderSwitcher.h
#import <UIKit/UIKit.h>
@interface MZMPhotoCalenderSwitcher : UIView <UIScrollViewDelegate>
@property (strong, nonatomic) UIScrollView *topSwitcher;
@end
Run Code Online (Sandbox Code Playgroud)
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];
}
}
Run Code Online (Sandbox Code Playgroud)
以下行
[view addSubview:button];
Run Code Online (Sandbox Code Playgroud)
加
view.contentSize = CGSizeMake(view.contentSize.width, button.frame.origin.y + button.frame.size.height);
Run Code Online (Sandbox Code Playgroud)
这会将滚动视图的内容大小设置为按钮的底部.
| 归档时间: |
|
| 查看次数: |
7703 次 |
| 最近记录: |