Not called button after adding in to container - ios

I have container view, where I add some image and button.
Container view is property of UIView.
I'll add my Container View to ScrollView like this:
#property (nonatomic,strong) UIView *containerView;
..
containerView = [[UIView alloc] init];
for (int i=0;i<5;i++) {
UIImageView *whiteBack = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 235-height, 385-height-height)];
whiteBack.center = CGPointMake(_scrollView.frame.size.width * i+_scrollView.frame.size.width/2, self.view.bounds.size.height/2-131+height);
whiteBack.backgroundColor = [UIColor darkGrayColor];
whiteBack.layer.cornerRadius = 3;
whiteBack.layer.masksToBounds = YES;
[containerView addSubview:whiteBack];
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 228-height, 340-height-height)];
img.center = CGPointMake(_scrollView.frame.size.width * i+_scrollView.frame.size.width/2, self.view.bounds.size.height/2-150+height);
img.image = [UIImage imageNamed:[NSString stringWithFormat:#"poster0%i.jpg",i+1]];
[containerView addSubview:img];
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeCustom];
infoButton.frame = CGRectMake(233, 0, 25, 25);
infoButton.center = CGPointMake(_scrollView.frame.size.width * i+_scrollView.frame.size.width/2-100, 325-height);
[infoButton addTarget:self action:#selector(flipImage:) forControlEvents:UIControlEventTouchUpInside];
infoButton.tag = i;
infoButton.backgroundColor = [UIColor clearColor];
[infoButton setImage:[UIImage imageNamed:#"upload-50"] forState:UIControlStateNormal];
[infoButton setTintColor:[UIColor whiteColor]];
[containerView addSubview:infoButton];
}
[self.scrollView addSubview:containerView];
///
- (void) flipImage :(id) sender {
NSLog(#"infoButton pressed:%i",[sender tag]);
}
but when I pressed button nothing happens!

Your container view has a zero frame (because you just did [[UIView alloc] init];). This means that it never receives any touches because you can never hit it. This goes for the subviews too.
The reason you see the subviews is that views, by default, do not clip drawing to their bounds.
To fix, set the frame of your container view to an appropriate size for the subviews you're adding.

Related

Wrong UIImageView dimensions on navigationItem

I am trying to add an image on navigation bar but the dimensions are wrong.
UIImageView* menuButton=[[UIImageView alloc]initWithFrame:CGRectMake(4, 7, 20, 20)];
menuButton.image=[UIImage imageNamed: Model.icon.MapList];
UIBarButtonItem *accountBarItem = [[UIBarButtonItem alloc] initWithCustomView:menuButton];
self.navigationItem.leftBarButtonItem = accountBarItem;
It seams that I don't have any control on the dimensions of the image, because when I change the values of initWithFrame nothing changes.
Also this problem appears only on iPhones greater than iPhone 5 model.
What am I missing?
You can just use simple button and set image for it.
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
[b setFrame:CGRectMake(0.0f, 0.0f, 25.0f, 25.0f)];
[b addTarget:self action:#selector(randomMsg) forControlEvents:UIControlEventTouchUpInside];
[b setImage:[UIImage imageNamed: Model.icon.MapList] forState:UIControlStateNormal];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:b];
self.navigationItem.leftBarButtonItem = item;
If you have wrong aspect ratio , you can use
[[b imageView] setContentMode: UIViewContentModeScaleAspectFit];
I'd recommend adding a border to your image view so you can see what's going on.
Here's what you're currently experiencing (using the imageView directly as the custom view of the bar button item):
- (void)_addImageViewToLeftButtonItemWithoutOuterView {
UIImageView *profileImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Homer.jpg"]];
[profileImageView setFrame:CGRectMake(0, 0, 40, 40)];
profileImageView.layer.cornerRadius = 20.0f;
profileImageView.layer.masksToBounds = YES;
profileImageView.clipsToBounds = YES;
profileImageView.layer.borderColor = [UIColor blackColor].CGColor;
profileImageView.layer.borderWidth = 1.0f;
UIBarButtonItem *rbi = [[UIBarButtonItem alloc] initWithCustomView:profileImageView];
self.navigationItem.leftBarButtonItem = rbi;
}
This is what it looks like without aspect fit:
And with aspect fit set:
And this is what I believe you want:
- (void)_addImageViewToLeftButtonItemWithOuterView {
UIView *profileView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
profileView.layer.borderColor = [[UIColor redColor] CGColor];
profileView.layer.borderWidth = 2.0f;
UIImageView *profileImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Homer.jpg"]];
profileImageView.contentMode = UIViewContentModeScaleAspectFit;
[profileImageView setFrame:CGRectMake(0, 0, 40, 40)];
profileImageView.layer.cornerRadius = 20.0f;
profileImageView.layer.masksToBounds = YES;
profileImageView.clipsToBounds = YES;
profileImageView.layer.borderColor = [UIColor blackColor].CGColor;
profileImageView.layer.borderWidth = 1.0f;
[profileView addSubview:profileImageView];
UIBarButtonItem *lbi = [[UIBarButtonItem alloc] initWithCustomView:profileView];
self.navigationItem.leftBarButtonItem = lbi;
}
Which results in this:
As you probably noticed, the sorta trick here is that you don't want to add the image view directly as the custom view within initWithCustomView of the UIBarButtonItem. Instead you want to use an outer view to hold the imageView, then you can adjust the frame of your subview to move it around.
Here's one more example if you wanted the image further to the right (obviously you won't want the border for the outer image view, but I've just left it there for demonstration purposes):
- (void)_addImageViewToLeftButtonItemWithOuterView {
// adjust this to 80 width (it always starts at the origin of the bar button item
UIView *profileView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
profileView.layer.borderColor = [[UIColor redColor] CGColor];
profileView.layer.borderWidth = 2.0f;
UIImageView *profileImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Homer.jpg"]];
profileImageView.contentMode = UIViewContentModeScaleAspectFit;
// and adjust this to be offset by 40 (move it to the right some)
[profileImageView setFrame:CGRectMake(40, 0, 40, 40)];
profileImageView.layer.cornerRadius = 20.0f;
profileImageView.layer.masksToBounds = YES;
profileImageView.clipsToBounds = YES;
profileImageView.layer.borderColor = [UIColor blackColor].CGColor;
profileImageView.layer.borderWidth = 1.0f;
[profileView addSubview:profileImageView];
UIBarButtonItem *lbi = [[UIBarButtonItem alloc] initWithCustomView:profileView];
self.navigationItem.leftBarButtonItem = lbi;
}

UIView alpha is disappering after UIButton is clicked

I have a container UIView and a UIView with alpha 0.7f in it, also a custom UIButton with image added to that container UIView. Now whenever I click on the button a big square chunk on transparent UIView disappears and stays that way. I have no idea why this happens. Can anyone help me figure this out?
UIWindow* window = [[UIApplication sharedApplication] keyWindow];
UIView *viewTestOverViewContainer = [[UIView alloc] initWithFrame:window.bounds];
UIView *viewOverlay = [[UIView alloc] initWithFrame:viewTestOverViewContainer.frame];
viewOverlay.backgroundColor = [UIColor blackColor];
viewOverlay.alpha = 0.7;
[viewTestOverViewContainer addSubview:viewOverlay];
float x = (window.bounds.size.width/2)-98;
float y = (window.bounds.size.height/2)-98;
UIView *viewTestPicContainer = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, 204, 204)];
viewTestPicContainer.backgroundColor = [UIColor whiteColor];
viewTestPicContainer.layer.cornerRadius = viewTestPicContainer.frame.size.width / 2;
viewTestPicContainer.clipsToBounds = YES;
viewTestPicContainer.backgroundColor = [UIColor whiteColor];
[viewTestPicContainer setContentMode:UIViewContentModeScaleAspectFill];
UIImageView *imgViewTestPic = [[UIImageView alloc] initWithFrame:CGRectMake(2, 2, 200, 200)];
imgViewTestPic.image = [UIImage imageNamed:#"portrait_eyes_14.jpg"];
imgViewTestPic.layer.cornerRadius = imgViewTestPic.frame.size.width / 2;
imgViewTestPic.clipsToBounds = YES;
imgViewTestPic.backgroundColor = [UIColor whiteColor];
[imgViewTestPic setContentMode:UIViewContentModeScaleAspectFill];
[viewTestPicContainer addSubview:imgViewTestPic];
[viewTestOverViewContainer addSubview:viewTestPicContainer];
UIButton *btnLeftArrow = [UIButton buttonWithType:UIButtonTypeCustom];
btnLeftArrow.frame = CGRectMake(46, 275, 12, 20);
[btnLeftArrow setImage:[UIImage imageNamed:#"popup-left-arrow"] forState:UIControlStateNormal];
[viewTestOverViewContainer addSubview:btnLeftArrow];
UIButton *btnRightArrow = [UIButton buttonWithType:UIButtonTypeCustom];
btnRightArrow.frame = CGRectMake(270, 275, 12, 20);
[btnRightArrow setImage:[UIImage imageNamed:#"popup-right-arrow"] forState:UIControlStateNormal];
[viewTestOverViewContainer addSubview:btnRightArrow];
[window addSubview:viewTestOverViewContainer];
Before:
After clicking the left arrow button:

How to make the SubView of a UIView, placed out of UIView frame, interactive?

I have added a UIView to a View of a View Controller. Say:
CGRect paneRect = {10.0f, 10.0f, 300.0f, 50.0f};
UIView *pane = [[UIView alloc] initWithFrame:paneRect];
pane.backgroundColor = [UIColor greenColor];
pane.userInteractionEnabled = YES;
pane.clipsToBounds = NO;
[self.view addSubview:pane];
Then I've added a UIButton to pane:
CGRect testRect = {10.0f, 25.0f, pane.frame.size.width - 20.0f, 50.0f};
UIButton *test = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[test setTitle:#"test" forState:UIControlStateNormal];
[test setFrame:testRect];
[pane addSubview:test];
Now half of the "test" button is within the pane and the other half is out. The upper half is interactive and responds to touches however the lower half is not.
Is it possible to make the whole "test" button interactive and touchable instead of its half?
If you really want to make these view stick with each other then you can do some thing like this
CGRect mainViewRect = {10.0f, 10.0f, 300.0f, 75.0f};
UIView *mainView = [[UIView alloc] initWithFrame:mainViewRect];
mainView.backgroundColor = [UIColor clearColor];
mainView.userInteractionEnabled = YES;
[self.view addSubview:mainView];
CGRect paneRect = {0.0f, 0.0f, 300.0f, 50.0f};
UIView *pane = [[UIView alloc] initWithFrame:paneRect];
pane.backgroundColor = [UIColor greenColor];
pane.userInteractionEnabled = YES;
pane.clipsToBounds = NO;
[mainView addSubview:pane];
CGRect testRect = {10.0f, 25.0f, pane.frame.size.width - 20.0f, 50.0f};
UIButton *test = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[test setTitle:#"test" forState:UIControlStateNormal];
[test setFrame:testRect];
[mainView addSubview:test];
just do Like this Way
CGRect paneRect = {10.0f, 10.0f, 300.0f, 50.0f};
UIView *pane = [[UIView alloc] initWithFrame:paneRect];
pane.backgroundColor = [UIColor greenColor];
pane.userInteractionEnabled = NO;
pane.clipsToBounds = NO;
[self.view addSubview:pane];
CGRect testRect = {10.0f, 25.0f, pane.frame.size.width - 20.0f, 50.0f};
UIButton *test = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[test setTitle:#"test" forState:UIControlStateNormal];
test.userInteractionEnabled = YES;
[test setFrame:testRect];
[pane addSubview:test];
For Action of UIButton just add
[test addTarget:self action:#selector(processButtonCLick) forControlEvents:UIControlEventTouchUpInside];
If you want to uibutton center of pane Add
test.center =pane.center;

Custom UIButton not working

After looking through the post on this simple topic I still can not figure out what I've done wrong here. I am trying to make the buttons loop through the same action in the same viewController.
When the view comes onto the screen the buttons do not click (or at least the NSLog does not write to the console which it does the first time it enters this code.)
- (IBAction)answer: (id) sender{
NSLog(#"The Number is: %d\n",qNumber);
qNumber+=1;
UIView *newView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[newView setUserInteractionEnabled:TRUE];
UIImage *buttonImage = [UIImage imageNamed:#"pink_button.png"];
UIImageView *buttonImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(23, 294, 72, 37)] autorelease];
[buttonImageView setImage:buttonImage];
UILabel* buttonLabel = [[[UILabel alloc] initWithFrame:CGRectMake(23, 294, 72, 37)] autorelease];
buttonLabel.text = #"newLow";
buttonLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size: 15.0];
buttonLabel.textColor = [UIColor blackColor];
buttonLabel.backgroundColor = [UIColor clearColor];
buttonLabel.textAlignment = UITextAlignmentCenter;
lowButton = [UIButton buttonWithType:UIButtonTypeCustom];
[lowButton addSubview:buttonImageView];
[lowButton addSubview:buttonLabel];
[lowButton addTarget:self action:#selector(answer:) forControlEvents:UIControlEventTouchUpInside];
[newView addSubview:lowButton];
self.view = newView;
}
Thanks for any help you can provide...even if I missed something simple :-)
Latest Updated Code:
lowButton = [UIButton buttonWithType:UIButtonTypeCustom];
[newView addSubview:buttonImageView];
[newView addSubview:buttonLabel];
[lowButton addTarget:self action:#selector(answer:) forControlEvents:UIControlEventTouchUpInside];
[newView addSubview:lowButton];
[self.view bringSubviewToFront:lowButton];
self.view = newView;
Still when the button is pressed it does not enter the action code.
You button needs a frame. You can't tap a frameless button.
lowButton.frame = CGRectMake(23, 294, 72, 37);
Also, when you add label and image to that button, their frame should have x and y 0.
UIImageView *buttonImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 72, 37)] autorelease];
...
UILabel* buttonLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 72, 37)] autorelease];
...
But may I ask why you are adding new UIImageView and UILabel to UIButton when it has it's label and background view properties?
Edit here is a cleaned-up code for you:
UIView *newView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
lowButton = [UIButton buttonWithType:UIButtonTypeCustom];
[lowButton setImage:[UIImage imageNamed:#"pink_button.png"] forState:UIControlStateNormal];
[lowButton setTitle:#"newLow" forState:UIControlStateNormal];
[lowButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[lowButton.titleLabel setFont:[UIFont boldSystemFontOfSize:15.0]];
[lowButton addTarget:self action:#selector(answer:) forControlEvents:UIControlEventTouchUpInside];
[newView addSubview:lowButton];
self.view = newView;
Notice I also removed [newView setUserInteractionEnabled:TRUE]; as it's not needed since it's enabled by default.
Your custom button's subviews are covering the button, so it does not get any clicks.
Try adding all the subviews to newView rather than to lowButton (including lowButton itself) and then call
[self.view bringSubViewToFront:lowButton];

How can I know which view I am clicking? And why the button no reaction?

I am designing a viewcontroller which have several button bar, each bar canbe clicked and show a content view.like:
http://i.stack.imgur.com/m5V4Q.png
When I click the buttonbar, it's frame will expand bigger, and you can see the content in it(which is a button).
First, a bar button(320*30 size) and a contentView is a set, they are subviews of listview. And several listview makes the whole view.
Second, when I click it, the list view will expand from 320*30 to 320*180, the contentview in it will expand from 300*0 to 300 * 130(20 pixel of margin). and I set the clipsToBounds of contentview to Yes to make sure the content in contentview won't show when the frame height is 0.
Now,the click can show the contentview exactly as I want. But here is a problem: I can't click the button in it, and I tried to set their userInteractionEnabled to yes . And even I set the contentview as user-defined,which I set the view's userInteractionEnabled to yes and overwrite the touchbegin function to show a alertView. Those test are failed, no reaction found. And I checked and sure that there shouldn't be any view covered it.
What's the reason might be?
The code of setup the list view is:
NSArray *array = [titleArray objectAtIndex:i];
NSString *ShenSuoTitle = [array objectAtIndex:0];
NSString *ShenSuoContent = [array objectAtIndex:1];
UIView *listView = [[UIView alloc] initWithFrame:CGRectMake(0, totalHeight, 320, ShenSuoViewHeight)];
[listView setBackgroundColor:[UIColor blackColor]];
[listView setTag:[[NSString stringWithFormat:#"%d",(i+1)] intValue]];
[self.scrollView addSubview:listView];
totalHeight = totalHeight + 1 + ShenSuoViewHeight;
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, ShenSuoViewHeight)];
[titleView setBackgroundColor:[UIColor whiteColor]];
[titleView setTag:[[NSString stringWithFormat:#"%d1",i+1] intValue]];
[listView addSubview:titleView];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTag:[[NSString stringWithFormat:#"%d2",i+1] intValue]];
[btn setFrame:CGRectMake(0, 0, 320, ShenSuoViewHeight)];
[btn addTarget:self action:#selector(reSetFrame:) forControlEvents:UIControlEventTouchUpInside];
[titleView addSubview:btn];
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(5, 12, 30, 30)];
img.image = [UIImage imageNamed:#"list_ico.png"];
[img setTag:[[NSString stringWithFormat:#"%d3",i+1] intValue]];
[titleView addSubview:img];
NSLog(#"img:%f,%f",img.frame.origin.y,img.frame.size.height);
UILabel *labTitle = [[UILabel alloc] initWithFrame:CGRectMake(35, 15, 100, 25)];
labTitle.textColor = [UIColor blackColor];
labTitle.backgroundColor = [UIColor clearColor];
labTitle.font = [UIFont boldSystemFontOfSize:15];
labTitle.text = ShenSuoTitle;
[titleView addSubview:labTitle];
NSLog(#"labTitle:%f,%f",labTitle.frame.origin.y,labTitle.frame.size.height);
//add a label for selected
UILabel *selectedLabel = [[UILabel alloc] initWithFrame:CGRectMake(214, 14, 86, 21)];
selectedLabel.textColor = [UIColor grayColor];
//selectedLabel.alpha = 0.75;
selectedLabel.backgroundColor = [UIColor clearColor];
selectedLabel.text = #"All";
selectedLabel.font = [UIFont boldSystemFontOfSize:15];
[selectedLabel setTag:[[NSString stringWithFormat:#"%d5",i+1] intValue]];
[titleView addSubview:selectedLabel];
NSLog(#"selectedLabel:%f,%f",selectedLabel.frame.origin.y,selectedLabel.frame.size.height);
UIView *content = [[UIView alloc] initWithFrame:CGRectMake(10, 10 + ShenSuoViewHeight, 300, 0)];
content.backgroundColor = [UIColor grayColor];
UILabel *testLa = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
testLa.text = #"Label";
UIButton *testBut = [UIButton buttonWithType:UIButtonTypeRoundedRect];
testBut.frame = CGRectMake(0, 40, 100, 30);
testBut.titleLabel.textColor = [UIColor greenColor];
testBut.titleLabel.text = #"Button";
content.tag = [[NSString stringWithFormat:#"%d4",i+1] intValue];
[content addSubview:testLa];
[content addSubview:testBut];
content.clipsToBounds = YES;
[testLa release];
[listView addSubview:content];
[content release];
[labTitle release];
[img release];
[titleView release];
[listView release];
the code handle the click to make the list view expands is:
UIView *titleView = (UIView *)[self.view viewWithTag:[[NSString stringWithFormat:#"%d1",i+1] intValue]];
titleView.backgroundColor = [UIColor colorWithRed:0.92 green:0.92 blue:0.92 alpha:0.9];
UIView *listView = (UIView *)[self.view viewWithTag:[[NSString stringWithFormat:#"%d",i+1] intValue]];
listView.frame = CGRectMake(0, totalHeight, 320, ShenSuoViewHeight);
totalHeight = totalHeight + 1 + 20 + 180 + ShenSuoViewHeight;
UIView *content = [self.view viewWithTag:[[NSString stringWithFormat:#"%d4",i+1] intValue]];
content.frame = CGRectMake(10, 10 + ShenSuoViewHeight, 300, 180);
UIImageView *img = (UIImageView*)[self.view viewWithTag:[[NSString stringWithFormat:#"%d3",i+1] intValue]];
img.image = [UIImage imageNamed:#"list_ico_d.png"];
I'm not sure I follow your situation exactly, and some code would be nice. To answer your question though, the reason is most likely that the touch is being intercepted elsewhere. It may also be that you just forgot to connect an action method to the button. Try using breakpoints or NSLogs to see what methods are being triggered and which aren't.
Thanks a lot. It is due to my fault. The code of expanding the bar which setting the listview's frame is wrong, it should add in the expanded height. This leads the visual is ok but the clicking not be handled.
So this problem closed.

Resources