How to add UIButton as bottomView to UIView? - ios

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

Related

Add a view at the top of another view iOS

I need help. I'm designing my UI, and I have been using the properties FRAME and CENTER to layout my views. Both of those are so much helpful to me. I also use the technique of adding my elements as subview of UIViews container.
But, right now, I want to place a UIView at the top of another. Please take a look at my picture. The buttons at the bottom of the screen is already set and is perfect right now, I'm only having a hard time to place my UIView (a container of my textfields) above the button/divider.
I'm only starting in iOS, I think 1 month plus already. I know basics of constraints programmatically but I don't want to use this for now as much as possible.
PS. I make my screen programmatically.
Sample of my code in this screen
// start adding container
UIView *container2 = [[UIScrollView alloc] init];
container2.frame = CGRectMake(0, 0, [TPRConstants screenWidth] * 0.95, heightOfScrollView);
container2.backgroundColor = [UIColor blueColor];
container2.contentSize = CGSizeMake([TPRConstants screenWidth] * 0.95, 250);
[self.view container2];
// stuffs and subviews/textfields inside the container
self.phoneTextField = [[UITextField alloc] init];
_phoneTextField.frame = CGRectMake(0, 200, scrollView.frame.size.width, 50);
_phoneTextField.delegate = self;
_phoneTextField.borderStyle = UITextBorderStyleNone;
_phoneTextField.background = [UIImage imageNamed:#"textfieldLogIn.png"];
_phoneTextField.backgroundColor = [UIColor clearColor];
[_phoneTextField setKeyboardType:UIKeyboardTypeNumberPad];
[container2 addSubview:_phoneTextField];
// add container for divider and log in button
UIView *container = [[UIView alloc] init];
container.frame = CGRectMake(0, 0, [TPRConstants screenWidth], 80);
container.backgroundColor = [UIColor whiteColor];
container.center = CGPointMake([TPRConstants screenWidth] * 0.50, [TPRConstants screenHeight] - container.frame.size.height/2);
[self.view addSubview:container];
// add divider
UIImageView *divider = [[UIImageView alloc] initWithFrame:CGRectMake(0, 10, container.frame.size.width, 5)];
divider.image = [UIImage imageNamed:#"dividerCP.png"];
divider.backgroundColor = [UIColor clearColor];
divider.contentMode = UIViewContentModeCenter;
[container addSubview: divider];
// add save login
UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
saveBtn.frame = CGRectMake(0, 0, container.frame.size.width * 0.95, 50);
saveBtn.center = CGPointMake(container.frame.size.width /2 , (container.frame.size.height - saveBtn.frame.size.height/2) - 10 );
[saveBtn addTarget:self action:#selector(saveBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[saveBtn setBackgroundImage:[UIImage imageNamed:#"logoutupCP.png"] forState:UIControlStateNormal];
[saveBtn setTitle:#"Save" forState:UIControlStateNormal];
[saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[saveBtn setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[saveBtn setBackgroundImage:[UIImage imageNamed:#"logoutdownCP.png"] forState:UIControlStateHighlighted];
[container addSubview: saveBtn];
In iOS everything in a view is added as a stack i.e on top of each other. So your button will always be on top of the divider as it is added last.
If you try your solution now on several screens, you will find that all of the items will be in different positions.
So you have to add constraints.
The easiest way is via storyboard.
Keep your textfields in one view and your button and divider in another view and add a vertical spacing constraint between both views.
Note: Ofcourse you have to add all the required trailing, leading, top and bottom constraints on each textField and the container views and separator image in view plus center horizontally and vertically constraints to the button.
If you wish to place a UIView at the base of phoneTextField use the maximum y position of phoneTextField for the y origin position of the UIView.
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(0,CGRectGetMaxY(phoneTextField.frame)+0,[TPRConstants screenWidth],50);
newView.backgroundColor = [UIColor blueColor];
[self.view addSubview:newView];
Then change the saveBtn frame to make space for newView:
saveBtn.frame = CGRectMake(0, CGRectGetMaxY(newView.frame), container.frame.size.width * 0.95, 50);

UIButton added as subview is away from UIView

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

Transparent Context in IOS?

I'm displaying an image in a UIScrollView but I want it to blend with a UIImageView which is laid behind the UIScrollView.
I'm currently trying to override - (void)drawRect:(CGRect)rect in a subclass of UIView created to display the image inside the scrollview, using either the method drawInRect:blendMode:alpha: or CGContextSetBlendMode but none worked. I think the reason is that the current context is opaque but I cannot find a workaround.
You can add UIImageView as a subclass of UIView (parent view of your scroll view).
You can change opacity of your UIImageView to match your criteria. If you just want a image as a background of your UIView you can set up background of your view and as a colour you can use [UIColor colorWithPatternImage:Your UIImage here].
Is it something you need?
//EXTENDED
I try it and both of the example below set the image as a background and when I scroll the image is not moving:
Example 1:
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"newtons_cradle.jpg"]]];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
[scrollView setContentSize:CGSizeMake(self.view.bounds.size.width, 1000.0f)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 300, 50)];
[label setBackgroundColor:[UIColor whiteColor]];
[label setText:#"Label Test Text"];
[scrollView addSubview:label];
[self.view addSubview:scrollView];
Example 2:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
[imageView setImage:[UIImage imageNamed:#"newtons_cradle.jpg"]];
[self.view addSubview:imageView];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
[scrollView setContentSize:CGSizeMake(self.view.bounds.size.width, 1000.0f)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 300, 50)];
[label setBackgroundColor:[UIColor whiteColor]];
[label setText:#"Label Test Text"];
[scrollView addSubview:label];
[self.view addSubview:scrollView]
;

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];

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