There are three sections in my UIView in the iOS application. May I know how can I only change to another view within one of the sections only?
For example:
When I press the next page button, section 2 will change to another UIView. However sections 1 and sections 3 will still remain.
Any help?
Thank you so much!
on Next Page button click function just do this
[pageSectionTwo removeFromSuperview];
[self addSubview:pageSectionTwoNewView];
"pageSectionTwo" this is the view which should be replaced to get another View in place
"pageSectionTwoNewView" this is the View which will show after you initial View in section 2 replaces
Add another UIButton, set its frame the same as UIButton in section 2, and hide it
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn2.frame = btn1.frame;
btn2.hidden = YES;
[self.view addSubView:btn2]; // This will add btn2 to you view
When you are ready to change your UIButtons, do the following
btn1.hidden = YES;
btn2.hidden = NO;
That will hide the first one and show the second.
Related
how to add a UIView in place of keyboard when we click accessory button using JSQ message view controller in swift
i am using JSQ message library in my app so i want add a UIView in which there are stack view which i want to show on on the click of accessary button in swift.
I created the View on storyboard, on click of accessory button this view will show.
I don't understand where this view will append in chat screen.
override func didPressAccessoryButton(sender: UIButton!){
self.inputToolbar.contentView.addView(View)
}
that will produce the nil exception
That my view i want to appent on the keyboard when accessary button click
I would suggest just adding your "Custom View" to the main view and giving it constraints to be where the keyboard is normally not actually adding it to the keyboard view.
Use the below code to have such effect and add your desired subview instead of demoView:
[self.inputToolbar.contentView.textView resignFirstResponder];
UIView *demoView = [UIView new];
demoView.backgroundColor = [UIColor redColor];
In below line you can decide the frame you want:
demoView.frame = CGRectMake(0, 0, self.inputToolbar.contentView.width/2, self.inputToolbar.contentView.height);
[self.inputToolbar.contentView addSubview:demoView];
And if you want some other kind of view like below:
[self.inputToolbar.contentView.textView resignFirstResponder];
UIView *demoView = [UIView new];
demoView.backgroundColor = [UIColor redColor];
demoView.frame = CGRectMake(0, 0, self.view.width, 200);
[self.view addSubview:demoView];
I have a segment control added to the navigation bar. It works fine. When i click on a segment (say index 0) it should load UIView A for me. and when i click on segment 1 it should load UIView B for me.
I have initialized UIVIew A in my .h file. and i am accessing it in the .m file as follows;
Here i am adding a Button and a label to View A
UIButton *buttonAdded= [UIButton buttonWithType:UIButtonTypeRoundedRect];
signUpButton.frame = CGRectMake(100, 100, 300, 50);
......
[self.viewA addSubview:buttonAdded];
// similarly added a label too
[self.viewA addSubview:labelAdded];
Now i added another Button and a Label to View B as the previous code;
// i have not shown how to declare a button and label, coz its same as above
[self.viewB addSubview:buttonAddedB];
[self.viewB addSubview:labelAddedB];
EDIT: I ADDED BOTH THESE VIEWS (View A & View B) IN THE VIEWDIDLOAD METHOD.
The problem is that when i click the segments nothing appears on the screen. Can someone please help me modify the code so it will work ?
Your viewA and viewB are NULL.
You have not allocated/initialized your viewA. There needs to be code somewhere that either hooks up a view from your nib (an IBOutlet), or you need something like this:
UIView *viewA = [[UIView alloc] initWithFrame:CGRectMake(x,y,width,height)];
Then you can use viewA.
I have a UITableViewController, when there is no data to populate the UITableView, I want to add a button, which uses an image. So, rather than the user seeing a tableview with no records, they will see an image that says, "No records have been added, Tap to add one", then they click and we create a new one.
I assumed I would just hide the UITableView, then create the button, but I never see the button. Here I am using:
if ([[fetchedResultsController sections] count] == 0) {
self.tableView.hidden = YES;
// Create button w/ image
UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 100, 50);
[btn setImage:[UIImage imageNamed:#"no-rides.png"] forState:UIControlStateNormal];
[self.view addSubview:btn];
}
Ideas on why I would never see the button? When I show this view, it seems to have a transparent background for a second, then changes white...
I am not sure if it works but you can try this:
If your view controller is not a UITableViewController and it contains a UITableView
[self.tableView removeFromSuperview]; then [self.view addSubview];
If your view controller is a UITableViewController, you may need to consider to set the first row to contain the image and text. Then, you can handle the event: tableView:didSelectRowAtIndexPath: and if user click on the first cell, you trigger the method handle the button event
In a UITableViewController, self.view could be self.tableView in which case hiding the table would also hide the button. Try using a custom UIViewController and creating either the table or the button as a subview of self.view instead.
Alternately, when there is no data, you can create a single custom cell containing your button and use that instead of normal cells.
I believe you can add the button to the table footer, as the table footer is shown even when there are no cells. (Note, this is different to a section footer.)
By default the tableFooterView property of the UITableView is nil. So just create your button, and then do:
self.tableView.tableFooterview = btn;
For all future travelers, I simply set .userInteraction = true (Swift) and it worked like a charm. All in all:
tableView.backgroundView = constructMyViewWithButtons()
tableView.backgroundView!.userInteraction = true
I'm trying to add a UIButton to a UIView, but am having some trouble with getting it to respond to touches.
I have a method which returns UIButtons after I provide it with a tag:
- (UIButton*)niceSizeButtonWithTag:(int)tag {
UIButton * aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setTag:tag];
[aButton addTarget:self action:#selector(buttonWasTapped:) forControlEvents:UIControlEventTouchUpInside];
CGRect newFrame = aButton.frame;
newFrame.size.width = 44;
newFrame.size.height = 44;
[aButton setFrame:newFrame];
return aButton;
}
As you can see I'm creating a new button and increasing the size.
I use this in the following way:
UIButton * anotherButton = [self niceSizeButtonWithTag:1];
[anotherButton setImage:[UIImage imageNamed:#"image" withExtension:#"png"] forState:UIControlStateNormal];
[anotherButton setCenter:CGPointMake(middleOfView)];
[aView addSubview:anotherButton];
I create a lot of buttons like this, hence the reason for the method.
The buttons are always created and added to the subview perfectly. I can see the image and they're in the correct position. The problem is that they only respond to touches in a tiny strip.
In this attached image,
alt text http://web1.twitpic.com/img/107755085-5bffdcb66beb6674d01bb951094b0e55.4c017948-full.png
The yellow shows the whole frame of the button,
The red shows the area that will respond to touches.
The grey shows a section of the view the button is added to.
If anyone could shed some light on why this is happening, it would be really useful.
UPDATE:
I should probably mention I'm trying to add this button to a UIView which is a subview of a UITableViewCell.
UPDATE 2:
Adding it directly to the UITableViewCell works, adding it to the UIView on the UITableViewCell is causing the error.
FINAL UPDATE
The problem turned out to be that I was sending the view containing the button to the back of the subviews on the UITableViewCell. Getting rid of the sendSubviewToBack: call fixed it.
Do you have any other views added to this containing view after you add the button? try setting some of your view's background color to blueColor, redColor and other colors to better see what the stack of views in your app is like. Then you should be easily able to see if there is some sort of view blocking the button.
Is it possible to set up a UITextField with a leftView so that if a user clicks into the UITextField the keyboard shows but if they click on an icon in the leftView another method is called (i.e., one that displays a UIPickerview)?
Have you checked that the leftFieldViewMode is set to something other than never
[textField setLeftViewMode:UITextFieldViewModeAlways];
Instantiate a button as u do normaly
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0,0,30,30)];
[btn setTitle:#"title" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(BtnClick) forControlEvents:UIControlEventTouchUpInside];
Instantiate a textfield as you do normally
UITextField *txtfield1=[[UITextField alloc]initWithFrame:CGRectMake(10, 10,300,30)];
[txtfield1 setBackgroundColor:[UIColor grayColor]];
[txtfield1 setPlaceholder:#" Enter"];
To set a view in left side(there is right view also available)
[txtfield1 setLeftView:btn];
if u want the view to show always then below code does it
[txtfield1 setLeftViewMode:UITextFieldViewModeAlways];
if u want the view to show never then
[txtfield1 setLeftViewMode:UITextFieldViewModeNever];
if u want the view to show if the textfield is not edited then below
[txtfield1 setLeftViewMode:UITextFieldViewModeUnlessEditing];
if u want the view to show if the textfield is edited then
[txtfield1 setLeftViewMode:UITextFieldViewModeWhileEditing];
button click event and pop over
-(void)BtnClick
{
//Usual pop over coding goes here
}
Have you tried using a UIButton for this task? From the documentation for the leftView property:
If your overlay view does not overlap
any other sibling views, it receives
touch events like any other view. If
you specify a control for your view,
the control tracks and sends actions
as usual. If an overlay view overlaps
the clear button, however, the clear
button always takes precedence in
receiving events.
So create a UIButton instance, configure its appearance and actions as needed, then set it as the leftView property.