我在主窗口上添加自定义视图然后在视图上创建一个按钮并且按钮正在显示但是按钮上的操作没有执行。 在按钮水龙头上,我想打开一个照相馆。 并在循环中创建按钮。
我正在使用的代码就是......
UIImage *image=[[UIImage alloc] initWithData:data cache:NO];
UIImageView *iv=[[UIImageView alloc] initWithFrame:CGRectMake(200, y, 75, 75)];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(200, y, 75, 75)];
[button setAlpha:0];
[button setTag:i];//change this to your loop counter
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
iv.image=image;
// iv.tag=@"bigimage.jpg";
iv.userInteractionEnabled=YES;
button.userInteractionEnabled = YES;
[iv addSubview:button];
[button release];
y=y+145;
[view1 addSubview:iv];
[iv release];
并尝试此代码,但没有改进.....
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(200, y, 75, 75);
UIImage *img = [[UIImage alloc] initWithData:data];
[button setBackgroundImage:img forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
y = y+145;
[view1 addSubview:button];
答案 0 :(得分:1)
按钮的边框不应与imageview具有相同的原点x和y。如果你想在图像视图中使用它,它应该具有原始x:0 y:0和与你的imageview相同的宽度和高度。
原点x,y是相对于它的父亲。在你的情况下是imageview。
答案 1 :(得分:0)
你好Shivangi,
I think you want to set image on button so try this code its will help you
//function to create button dynamically on scrollview
-(void)createButton
{
int Y = 20;
indexRow = 0;
for (int i = 0; i < [prow.noOfPhotos count]; i++)
{
thumbNailButton = [UIButton buttonWithType:UIButtonTypeCustom];
int X = 4 + (78 * (i % 4));
NSLog(@"X = %d",X);
NSLog(@"Start Y = %d",Y);
if (i % 4 == 0)
{
NSLog(@"%d",i);
Y = (70 * verticalImageRowCount);
NSLog(@"Y = %d",Y);
verticalImageRowCount++;
NSLog(@"VerticalImageRowCount= %d");
}
CGRect rect = myScrollView.frame;
rect.size.width = 100;
rect.size.height = Y + 77;
myScrollView.contentSize = CGSizeMake(rect.size.width,rect.size.height);
thumbNailButton.frame = CGRectMake(X,Y, 62,65);
view = [[UIImageView alloc] init];
prow.photo = [prow.noOfPhotos objectAtIndex:indexRow];
imagePath = prow.photo;
asyncImageView = [[[AsyncImageView alloc] initWithFrame:CGRectMake(4, 0, 62, 65)] autorelease];
asyncImageView.backgroundColor = [UIColor clearColor];
[view addSubview:asyncImageView];
NSURL *url = [NSURL URLWithString:imagePath];
[asyncImageView loadImageFromURL:url];
[view setImage:[UIImage imageNamed:[prow.noOfPhotos objectAtIndex:i]]];
thumbNailButton.tag = i;
thumbNailButton.backgroundColor=[UIColor clearColor];
[thumbNailButton addTarget:self action:@selector(imageClicked:) forControlEvents:UIControlEventTouchUpInside];
[thumbNailButton addSubview:view];
[myScrollView addSubview:thumbNailButton];
indexRow+= 1;
}
}
答案 2 :(得分:0)
尝试在您的buttonPressed中放置一个NSLog:并查看该方法是否被调用。也许该方法被调用,但照片库应用程序由于其他原因没有启动。
答案 3 :(得分:0)
如果将按钮的alpha设置为0,则用户无法单击该按钮。这意味着不会触发该事件。
[button setAlpha:0];
这与设置button.hidden = YES具有相同的效果。
如果你想要一个不可见的点击区域,使用按钮,将按钮类型设置为UIButtonTypeCustom,它应该这样做。