UIButton added as subview is away from UIView - ios

Tried and searched a lot. Button added as subview is away from view when the frame of superview is small. I dont want it to be appear when frame is small.
UIView *vw = [[UIView alloc] initWithFrame:CGRectMake(20 , 100, 200, 30)];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
[btn setTitle:#"OK" forState:UIControlStateNormal];
[btn setFrame:CGRectMake(20 , 100, 30, 20)];
[vw sendSubviewToBack:btn];
[vw addSubview:btn];
[vw setBackgroundColor:[UIColor redColor]];
[self.view addSubview:vw];
this is my code. It look like this

Look on the frame of the UIButton. Its origin.y is 100px. It should be 0, if you want to add it to the vw.

You did wrong. Before adding btn to view, you've called sendSubviewToBack:. Just rewrite as below.
[vw addSubview:btn];//First
[self.view addSubview:vw];//second
[vw sendSubviewToBack:btn];//Third
You don't want to appear if it's lie outside superview, use this. vw.clipsToBounds = YES

Related

How to add UIButton as bottomView to UIView?

I have a UIView class, and in xib also I have a MainView which is a UIView. I want to add UIButton as bottomView to the View.I need to give bottom space 16 between MainView and bottomView. How do I do it?
self.bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, self.MainView.bounds.size.height +30, self.MainView.bounds.size.width, 40.0)];
[self.bottomView setBackgroundColor:[UIColor greenColor]];
self.selectBtn= [UIButton buttonWithType: UIButtonTypeRoundedRect];
[self.selectBtn setFrame:CGRectMake(0,20,100,40)];
[self.selectBtn setTitle:#"Select" forState:UIControlStateNormal];
[self.selectBtn addTarget:self action:#selector(selectButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.bottomView addSubview:self.selectBtn];
[self addSubview:self.bottomView];
Expected result
Output from above code
Please find you have added button on this frame
self.bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, self.MainView.bounds.size.height +30, self.MainView.bounds.size.width, 40.0)];
Here you have made the y as height + 30 so it is not visible to the view.
You should use this.
self.bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, self.MainView.bounds.size.height - 40, self.MainView.bounds.size.width, 40.0)];
Also if you want 16 pixel gap you should the view above it with the same approach.
Please let me know if you need any more help

How to show the title under the UIButton?

I have used the title inset to display the text name under the UIButton.
Here's my code and the output:
[button setTitleEdgeInsets:UIEdgeInsetsMake(60, 0, 0, 0)];
Here's my expected output:
I have used the UIButton and set the title and UIImage as background. So how to show the title label under the UIButton?
What you're trying to do is correct; you just need to set the inset edges as done below. Then you need to increase your button frame so that it can hold the title and image in the same time.
[button setTitleEdgeInsets:UIEdgeInsetsMake(60, 0, 0, 0)];
[button setImageEdgeInsets:UIEdgeInsetsMake(0.0f, 0.f, button.titleLabel.frame.size.height, 0.f)];
This can also be done via storyboards as #VD Patel suggests that is up to you which approach you prefer.
You could also do this with a separate UIImageview and a button that would be easier to handle and would be more efficient.
Please Select button in xib first. then select Attribute Inspector, in this Select Title in "Edge" and set appropriate "Inset" as per Requirements.
please take a look with below code and set insets as per your Requirements.
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(0, 0, self.view.bounds.size.width, 40);
[myButton setImage:[UIImage imageNamed:#"imageName"] forState:UIControlStateNormal];
[myButton setTitle:#"Rate us on app store" forState:UIControlStateNormal];
[myButton setTitleEdgeInsets:UIEdgeInsetsMake(2, 50, 2, 20)];//set ur title insects myButton
[myButton setImageEdgeInsets:UIEdgeInsetsMake(2, -200, 2, 2)];//make negative edge for left side
[myButton setBackgroundColor:[UIColor greenColor]];
[self.view myButton];
Create an UIImageView,UILabel and UIButton. And later add ImageView and Label as subview to Button.
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, 200, 100)];
lbl.textAlignment = NSTextAlignmentCenter;
[lbl setText:#"Mono"];
UIImageView * imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
imgView.image = [UIImage imageNamed:#"your_image.png"];
[imgView setUserInteractionEnabled:NO];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn addSubview:imgView];
[btn addSubview:lbl];
[btn setFrame:CGRectMake(0, 20, 200, 300)];
[btn addTarget:self action:#selector(OnButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
Try adding this code in viewDidLoad Method.

adding multiple subviews in 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];

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.

Why is UIButton not responding to touches?

I've look at many responses and still cannot figure it out. I know the first line is a little weird, but its because it is inheriting from a super class that sets the headerview to search bar
searchBar = (UISearchBar *)self.tableView.tableHeaderView;
[[searchBar.subviews objectAtIndex:0] removeFromSuperview];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, -100, self.view.frame.size.width, 100)];
label.text = #"AAAA";
[searchBar addSubview:label];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethod)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, -60, 160.0, 40.0);
searchBar.exclusiveTouch = NO;
searchBar.userInteractionEnabled = NO;
label.userInteractionEnabled = NO;
button.userInteractionEnabled = YES;
[searchBar insertSubview:button atIndex:3];
userInteractionEnabled set to NO on a parent view will cascade down to all subviews. Thus, your instance of UIButton will appear unresponsive because the searchBar is disabled.
Here is your answer :
searchBar.userInteractionEnabled = NO;
If you are going to add a subview to a view that has userInteraction disabled then that subview won't receive touches. I haven't looked very good at your code to see if there are other mistakes.
Look at the frames in the first place :
button.frame = CGRectMake(80.0, -60, 160.0, 40.0);
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, -100, self.view.frame.size.width, 100);
The button isn't visible at all , and the label the same , the objects are placed outside of bounds and that's why you cannot touch them.

Resources