adding multiple subviews in ios - ios

I'm trying to add multiple subview programmatically, but my buttons inside those subviews are not working. The buttons were also added as subviews programmatically.
Here's the code, hopefully will explain more about what I mean.
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
[self setBackgroundColor:[UIColor clearColor]];
// UIView container of selected image with comment button.
UIView *containerOfSelectedImage = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 0, 350)];
NSLog(#"%f", self.frame.size.width);
// image view of selected photo by user.
UIImageView *userImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"nature_01.jpg"]];
[userImage setFrame :CGRectMake(0, 0, 340, 300)];
// comment button with action of recodering user's comment.
UIButton *commentButton = [UIButton buttonWithType:UIButtonTypeCustom];
//[commentButton setBackgroundColor : [UIColor clearColor]];
float y = userImage.frame.size.height + 5.0f;
[commentButton setFrame : CGRectMake(32.0f, y, 24.0f, 24.0f)];
[commentButton addTarget:self action:#selector(audioRecordAction) forControlEvents:UIControlEventTouchDown];
[commentButton setImage:[UIImage imageNamed:#"comments-24.png"] forState:UIControlStateNormal];
[containerOfSelectedImage addSubview:userImage];
[containerOfSelectedImage addSubview:commentButton];
[self addSubview:containerOfSelectedImage];
[self addSubView:self.commentContainerView];
return self;

You need to increase the frame of the containerOfSelectedImage. Currently the width is 0.
The containerOfSelectedImage's frame has to be big enough so the button fits inside it. If the button is outside that frame it will show up but you can't touch him.
To debug this issues you can use a tool like Reveal App. If any of your buttons is outside the frame of the parent then the touch will not be detected.

Change [commentButton addTarget:self action:#selector(audioRecordAction) forControlEvents:UIControlEventTouchDown]; to [commentButton addTarget:self action:#selector(audioRecordAction) forControlEvents:UIControlEventTouchUpInside];

Related

in landscape no showing image and button

I have added one imageview, 2 button in one view controller in code. In portrait mode its working well. But in landscape mode not able to see the image and button.
#implementation ViewController {
UIImageView* imgView; // your UIImageView
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[self navigationController] setNavigationBarHidden:YES animated:YES];
UIImage* img1 = [UIImage imageNamed:#"11.jpg"];
UIImage* img2 = [UIImage imageNamed:#"s2.jpg"];
imgView = [[UIImageView alloc] initWithFrame:self.view.frame]; // create a UIImageView with the same size as the view.
imgView.animationImages = #[img1, img2]; // use a nice NSArray literal to pass in your images.
imgView.animationDuration = 2.0*2.0; // 2 seconds for each image (x3 images)
[self.view addSubview:imgView]; // add UIImageView to view
[imgView startAnimating]; // start animating
// button one
UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(0.0, self.view.frame.size.height-50, self.view.frame.size.width/2 , 50.0)];
[button setBackgroundColor:[UIColor colorWithRed:188/255.0f green:155/255.0f blue:211/255.0f alpha:0.6f]];
[button setTitle:#"Sign Up" forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; // title color
[button addTarget:self action:#selector(method:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
// Button Two
UIButton* button1 = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height-50, self.view.frame.size.width/2 , 50.0)];
[button1 setBackgroundColor:[UIColor colorWithRed:188/255.0f green:155/255.0f blue:211/255.0f alpha:0.6f]];
[button1 setTitle:#"Sign In" forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(method:) forControlEvents:UIControlEventTouchUpInside];
[button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; // title color
[self.view addSubview:button1];
// Border color for one button
UIView *leftBorder = [[UIView alloc] initWithFrame:CGRectMake(0, 1, 1, button.frame.size.height)];
leftBorder.backgroundColor = [UIColor colorWithRed:237/255.0f green:211/255.0f blue:246/255.0f alpha:0.6f];
[button1 addSubview:leftBorder];
}
Why its not working well for landscape. Please help me, for portrait its working well. For landscape its not working ..
It's not working in landscape because when we rotate to landscape the button and image view, as you have positioned them in your code, are located below the bottom of the screen.
You will have to implement Auto Layout. It will fix the problem. Please avoid hard coding the positions as will create problems in UI. If you want to hard code the points then make your app in either portrait mode or landscape mode.
In auto layout constraints can be set programmatically as well. Just check which is feasible for you and implement it.

Adding a close button to the modal view that is partially off the view

I am trying to add a "X" button to the top of the modal view so if the user presses that it closes the modal view. I looked at the solution proposed here how to add close button to modal view corner which is presented in UIModalPresentationPageSheet?
But I have two follow up questions here as i am fairly new to ios programming
The solution proposed in the above page does not seem to work for me. I tried it with a normal button first in the viewDidLoad of the modal VC and I am seeing it appear only within the bounds of modal view and not outside as I want.
UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
buttonView.backgroundColor = [UIColor brownColor];
CGRect buttonFrame = CGRectMake( 0, 0, 100, 50 );
UIButton *button = [[UIButton alloc] initWithFrame: buttonFrame];
[button setTitle: #"Close" forState: UIControlStateNormal];
[button setTitleColor: [UIColor redColor] forState: UIControlStateNormal];
[button addTarget:self
action:#selector(closeButtonPressed)
forControlEvents:UIControlEventTouchUpInside];
[buttonView addSubview: button];
CGRect parentView = self.view.superview.frame;
CGRect currentView = self.view.frame;
CGRect buttonViewFrame = buttonView.frame;
buttonViewFrame.origin = CGPointMake(parentView.origin.x - buttonViewFrame.size.width/2.0f, parentView.origin.y - buttonViewFrame.size.height/2.0f);
[buttonView setFrame:buttonViewFrame];
[self.view addSubview:buttonView];
what I am seeing is
How do I draw the "X" button do I use CGRect to draw it our or instead of adding a button in the above example should I just add a image with the "X" in it as the subview?
thanks in advance for the help
do like this, hope helps u :)
//adding button to your view
UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(100, 200, 300, 350)];
aView.backgroundColor = [UIColor blueColor];
aView.tag = 100;
[self.view addSubview:aView];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setTitle:#"CLOSE" forState:UIControlStateNormal];
[aButton addTarget:self action:#selector(tapped:) forControlEvents:UIControlEventTouchUpInside];
aButton.frame = CGRectMake(aView.bounds.origin.x, aView.bounds.origin.y, 80, 30);// adjust with respect to bounds
aButton.backgroundColor = [UIColor redColor];
[aView addSubview:aButton];
[aView release]; //i am doing without ARC
The other option is to create a larger view and put your UITableView within this, along with your close button. The close button can then be at position (0,0) and the UITableView can start at (25,25).
This prevents complications - I have seen clipped views fail to recognise the touch outside of the main view, so your 50x50 button only has a touch area of 25x25.

UIButtons receiving touch events when added directly to scrollview but not indirectly

I have 10 UIButtons stacked side to side in my app , and the total dimensions of the buttons are 640x96.
If I add these buttons directly to the UIScrollView, they register touch events and are scrollable.
But if I try to add them to a plain UIView, then add that UIView to the scrollview, they dont work.
This is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *fview = [[UIView alloc] init];
fview.frame = CGRectMake(0, 0, 640, 96);
fview.backgroundColor = [UIColor blackColor];
[fview setUserInteractionEnabled:YES];
[_sv setUserInteractionEnabled:YES];
for (int i=0; i<10; i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(64*i, 0, 64, 96);
[button setTitle:#"Click Me!" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[fview addSubview:button];
}
[_sv addSubview:fview];
_sv.contentSize = CGSizeMake(640, 96);
}
-(void)buttonClicked:(id)sender {
NSLog(#"Clicked!");
}
While I was writing the question I fixed the problem :D
All I had to do was to set the frame of the UIView before I set the scrollview's contentsize.
In my case:
[_sv addSubview:fview];
fview.frame = CGRectMake(0, 0, 640, 96); // Added line
_sv.contentSize = CGSizeMake(640, 96);
Hope this helps!

Adding Button from loop into UIScrollView

i am writing this code to create buttons from loop and adding them into UIScrollView but somehow its not working , please can someone help.
the code is
scroll = [[UIScrollView alloc]init];
[self createurdulistfordisplay];
for(int i = 1;i<[urdulist count];i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:#"2.png"] forState:UIControlStateNormal];
[button addTarget:self
action:#selector(playurduaudio:)
forControlEvents:UIControlEventTouchDown];
button.frame = CGRectMake(80.0, 210.0 + (30*i), 213.0, 46.0);
[scroll addSubview:button];
}
I think you have forgotten to add scroll to view. add like his at last line
[self.view addSubView:scroll];
Here you have created scroll but not added to your view that means until you adds this scrollView to main view i.e. to self.view ,you will not get it display on screen

Need to add multiple UI elements to tableheaderview of UITable in iOS

I have a UITableViewController that has a tableView. To this tableView, I would like to add both a label as well as a button but unfortunately, I am only able to add one or the other.
Here is my code that I am working with:
- (void) viewDidLoad {
[super viewDidLoad];
selectAllButton = [UIButton buttonWithType:UIButtonTypeCustom];
[selectAllButton setFrame:CGRectMake(280, 0, 25, 25)];
[selectAllButton setImage:[UIImage imageNamed:#"CheckBox1.png"] forState:UIControlStateSelected];
[selectAllButton setImage:[UIImage imageNamed:#"CheckBox2.png"] forState:UIControlStateNormal];
[selectAllButton setUserInteractionEnabled:YES];
[selectAllButton addTarget:self action:#selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
selectAllLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 40)];
[selectAllLabel setTextColor:[UIColor blackColor]];
[selectAllLabel setBackgroundColor:[UIColor clearColor]];
[selectAllLabel setText:#"Select All"];
self.tableView.tableHeaderView = selectAllButton;
self.tableView.tableHeaderView = selectAllLabel;
}
When I run the above method, I am only able to see my label which tells me it is replacing the button. Is there a way for me to get both elements to appear in the tableheaderview?
Create a UIView, add your button & label to it as subviews, than make that view your self.tableview.tableHeaderView
...your code
UIView *headerView = [UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, selectAllButton.frame.size.height + selectAllLabel.frame.size.height)];
[headerView addSubview:selectAllButton];
[headerView selectAllLabel];
self.tableView.tableHeaderView = headerView;
When you init your button & label be sure to change the frame as they will be based on the UIView
Since tableHeaderView is an UIView, try to add it with addSubview:(UIView*) or create an UIView and add that as your tableHeaderView.

Resources