How to present 'UIViewController' in Cocoas2D with custom size - ios

Imagine I have dialog and size of this dialog is 200x200 and I want to present on the iPhone screen temporary (can dismiss with button).
I try it. but the view controller that I presented is replace current scene (dialog and white background border).
I want to present with custom size (200x200) at center of iPhone screen and see my content as background
I try to set dialog's background colour to clearColor and It doesn't work.
Thanks in advance.

Create your button like this,
[button addTarget:self action:#selector(ButtonFun) forControlEvents:UIControlEventTouchUpInside];
-(void)ButtonFun {
UIView *newView1 = [[UIView alloc] initWithFrame:CGRectMake(0,0,200,200)];
newView1.backgroundColor = [UIColor whiteColor];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
textView.text = #"Write your dialog";
[textView setFont:[UIFont fontWithName:#"ArialMT" size:15]];
textView.textAlignment = NSTextAlignmentCenter;
[newView1 addSubview:textView];
newView1.center = self.view.center;
[self.view addSubview:newView1];
}
To remove this UIView use UITapGestureRecognizer,or simply
[newView1 removeFromSuperview];

Related

How to I add a button on top of iCarousel?

I added the date label and the share button via code. Now I want to add a static button in the right top corner to open a slide out menu(I'm using MMDrawerController). If I'm adding the via code, it's moving with the carousel.
If I'm adding a button on the name label(My App) on the storyboard, it's not getting clicked and the carousel gesture is being registered instead of the button click.
How should I proceed to add a button for a slide out menu on the right ?
-(UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view{
UIView *magView = nil;
CGFloat height = self.myCarousel.bounds.size.height;
CGFloat width = self.myCarousel.bounds.size.width;
//Magazine View
magView = [[UIView alloc] initWithFrame:CGRectMake(0, 40, width/1.35, height/1.75)];
magView.backgroundColor = [UIColor clearColor];
UIImageView *magImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"4 1:2.jpg"]];
magImage.frame = CGRectMake(0, 0, width/1.35, height/1.75);
[magView addSubview:magImage];
//Date Label
UILabel *dateLabel = nil;
dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, height/1.7, width/4, height/25)];
dateLabel.backgroundColor = [UIColor clearColor];
dateLabel.text = #"12-03-2017";
dateLabel.font = [UIFont fontWithName:#"Roboto-Black" size:5];
dateLabel.textColor = [UIColor whiteColor];
//Share Button
UIButton *shareButton = nil;
shareButton = [[UIButton alloc] initWithFrame:CGRectMake(width/1.49, height/1.7, height/25, height/25)];
shareButton.backgroundColor = [UIColor clearColor];
[shareButton setBackgroundImage:[UIImage imageNamed:#"share_icon_1x"] forState:UIControlStateNormal];
[magView addSubview:dateLabel];
[magView addSubview:shareButton];
return magView;
}
Simulator
Storyboard
How do I proceed to add the button ?
Add button to storyboard and after adding carousel programmatically. add code to bring the button to top on view.
[self.view bringSubviewToFront:button];
if you are adding it programmatically then just after [self.view addSubview:carousel];
call [self.view bringSubviewToFront:button](make sure button is added on view before calling it)
I solved the issue. Instead of taking the view of the ViewController I dragged/dropped another view onto the ViewController and displayed the carousel on it. Now I can set the button easily. Thanks for the help though. Cheers
It will help you
[xyzButton.superview setUserInteractionEnabled:YES];
:)

Is it possible to dim a UIView but allow touch of elements behind it?

How would you have a grey overlay on top, but allow to touch buttons beneath it?
You can set userInteraction of overlay to false.
As mentioned, you can set userInteractionEnabled to NO.
But pay attention: when you create a full screen transparent view, and set its userInteractionEnabled to NO, all the subviews of that view will not be responsive to user actions. If you add a button on your view, it will not respond to user taps! and another problem: if you set the alpha value of that view to be transparent, e.g. 0.4, then all its subviews will be transparent too!
The solution is simple: don't put any subview on your transparent view, but instead, add your other elements as siblings of your view. Here is Objective-C code to show what I mean: (notice the comments which say it all):
//Create a full screen transparent view:
UIView *vw = [[UIView alloc] initWithFrame:self.view.bounds];
vw.backgroundColor = [UIColor greenColor];
vw.alpha = 0.4;
vw.userInteractionEnabled = NO;
//Create a button to appear on top of the transparent view:
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 80, 44)];
btn.backgroundColor = [UIColor redColor];
[btn setTitle:#"Test" forState:UIControlStateNormal];
[btn setTitle:#"Pressed" forState:UIControlStateHighlighted];
//(*) Bad idea: [vw addSubview:btn];
//(**) Correct way to have the button respond to user interaction:
// Add it as a sibling of the full screen view
[self.view addSubview:vw];
[self.view addSubview:btn];

UILabel not showing subview in iOS8

Currently I made a back Label for background and add UIButton on UILabel. It's showing in iOS 7 but in iOS 8 it's not showing . If I replace UILabel with UIView then It's working fine.
If I use UILabel in iOS 8 using this code For showing UILabel and subview UIButton
UILabel *downLabel = [[UILabel alloc]init];
downLabel.frame = CGRectMake(0, SCREEN_HEIGHT*0.92, SCREEN_WIDTH,SCREEN_HEIGHT*0.08 );
[downLabel setBackgroundColor:[UIColor colorWithRed:(66/255.f) green:(67/255.f) blue:(63/255.f) alpha:1]];
downLabel.userInteractionEnabled = YES;
[self.view addSubview:downLabel];
UIButton *downbtnobj = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*0.68,downLabel.frame.size.height/10,SCREEN_WIDTH*0.30,downLabel.frame.size.height/1.25)];
UIImage *btndownImage = [UIImage imageNamed:#"img 3.png"];
[downbtnobj setImage:btndownImage forState:UIControlStateNormal];
[downbtnobj addTarget:self action:#selector(bulletInButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[downLabel addSubview:downbtnobj];
Now You can see only UILabel is showing but button is not showing on UILabel.
One another this If I replace UILabel by UIView then It's showing perfect.
UIView *downLabel = [[UIView alloc]init];
downLabel.frame = CGRectMake(0, SCREEN_HEIGHT*0.92, SCREEN_WIDTH,SCREEN_HEIGHT*0.08 );
[downLabel setBackgroundColor:[UIColor colorWithRed:(66/255.f) green:(67/255.f) blue:(63/255.f) alpha:1]];
downLabel.userInteractionEnabled = YES;
[self.view addSubview:downLabel];
UIButton *downbtnobj = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*0.68,downLabel.frame.size.height/10,SCREEN_WIDTH*0.30,downLabel.frame.size.height/1.25)];
UIImage *btndownImage = [UIImage imageNamed:#"img 3.png"];
[downbtnobj setImage:btndownImage forState:UIControlStateNormal];
[downbtnobj addTarget:self action:#selector(bulletInButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[downLabel addSubview:downbtnobj];
In this code you can see I replace only UILabel by UIView. Now button is showing.
Can any help why subviews not showing on UILabel in iOS8
It's not clear what is different about labels in iOS 8, but if you call layoutIfNeeded on your label, that fixes the problem (it needs to be after you set the frame),
UILabel *downLabel = [[UILabel alloc]init];
downLabel.frame = CGRectMake(0, SCREEN_HEIGHT*0.92, SCREEN_WIDTH,SCREEN_HEIGHT*0.08 );
[downLabel layoutIfNeeded];
[downLabel setBackgroundColor:[UIColor colorWithRed:(66/255.f) green:(67/255.f) blue:(63/255.f) alpha:1]];
downLabel.userInteractionEnabled = YES;
[self.view addSubview:downLabel];
After Edit:
I also found out that if you set the text (any time after you create the label), then the button appears.
Very Simple way you need set clipsToBounds on your UILabel
downLabel.clipsToBounds = YES;

UITextFields aren't allowing me to edit text

I've programmatically created two UITextFields in my iOS app, and set their text to the _minPrice and _minPrice variables, respectively.
The _minPrice and _maxPrice values appear correctly in the two fields, but tapping on them doesn't allow the user to edit them, they just remain those static values, backspacing doesn't work. Is there anything about my code thats preventing the text fields from being edited?
// Min Price
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(-25, -76, 70, 30)];
tf.textColor = [UIColor blackColor];
tf.font = [UIFont fontWithName:#"Helvetica-Neue" size:14];
tf.backgroundColor=[UIColor whiteColor];
tf.text= _minPrice;
tf.textAlignment = NSTextAlignmentCenter;
tf.layer.cornerRadius=8.0f;
tf.layer.masksToBounds=YES;
tf.layer.borderColor=[[UIColor lightGrayColor]CGColor];
tf.layer.borderWidth= 1.0f;
// Max Price
UITextField *tf1 = [[UITextField alloc] initWithFrame:CGRectMake(100, -76, 70, 30)];
tf1.textColor = [UIColor blackColor];
tf1.font = [UIFont fontWithName:#"Helvetica-Neue" size:14];
tf1.backgroundColor=[UIColor whiteColor];
tf1.text= _maxPrice;
tf1.textAlignment = NSTextAlignmentCenter;
tf1.layer.cornerRadius=8.0f;
tf1.layer.masksToBounds=YES;
tf1.layer.borderColor=[[UIColor lightGrayColor]CGColor];
tf1.layer.borderWidth= 1.0f;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 200, 400, 400)];
[view addSubview:tf];
[view addSubview:tf1];
[self.view addSubview:view];
Your issue is clearly the frames you're setting...
Setting the color of the view you add the labels to to blue reveals your problem:
If you ensure that the labels are actually within the view you add them to (i.e. not negative), editing will be fine. All I did was change the negative x and y values in tf to positive, and they were editable:
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(25, 76, 70, 30)];
Try this! Maybe there is another view at the top of the textField
your_textfield.userInteractionEnabled = YES;
If this doesn't work
add another line
[self.view bringSubviewToFront: your_textfield];
Try to add delegate methods on your textfields. Like
- (void) textFieldDidEndEditing:(UITextField *)textField
{
// ADD BREAKPOINT HERE.
}
Check if it goes to that line of code. If not maybe there's a view on top of it. Or you can try to bring textfield to front like .
[self.view bringSubviewToFront:yourTextfield];
But this isn't a good example of how you fix the problem. Just to test if there is a view on top of it.

Add a sticky view underneath the Navigation Bar

I've seen a few other examples, but I haven't seen anything that actually sticks. What I want is essentially what a tableViewHeader does. I have a tableViewController with a navigation bar. I want to put a view into place that shows some search criteria in a small bar directly below the navbar. But I need it to stick when the user swipes to view the results.
I've tried adding a UIView as a subview to the navigation bar, but it always sits at the top of the Navigation Bar, overlaying everything.
Here's what worked:
// Create a custom navigation view
_navigationView = [[UIView alloc] init];
_navigationView.backgroundColor = [UIColor whiteColor];
_navigationView.frame = CGRectMake(0.0f,
self.navigationController.navigationBar.frame.origin.y + 44.0f,
self.view.frame.size.width,
30.0f);
// Create bottom border for the custom navigation view
_navigationViewBorder = [[UIView alloc] init];
_navigationViewBorder.backgroundColor = [UIColor darkGrayColor];
_navigationViewBorder.tag = 1;
_navigationViewBorder.frame = CGRectMake(0.0f,
self.navigationController.navigationBar.frame.origin.y + 74.0f,
self.view.frame.size.width,
0.5f);
// Add labels for date, results, and the time range
_dateDisplay = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 70, 15)];
[_dateDisplay setFont:[UIFont fontWithName:#"HelveticaNeue" size:13]];
[_dateDisplay setText:#""];
_timeRangeDisplay = [[UILabel alloc] initWithFrame:CGRectMake(98, 0, 135, 15)];
[_timeRangeDisplay setFont:[UIFont fontWithName:#"HelveticaNeue-Bold" size:13]];
[_timeRangeDisplay setText:#""];
_resultsDisplay = [[UILabel alloc] initWithFrame:CGRectMake(240, 0, 80, 15)];
[_resultsDisplay setFont:[UIFont fontWithName:#"HelveticaNeue" size:13]];
[_resultsDisplay setText:#""];
[_navigationView addSubview: _dateDisplay];
[_navigationView addSubview: _timeRangeDisplay];
[_navigationView addSubview: _resultsDisplay];
// Add two views to the navigation bar
[self.navigationController.navigationBar.superview insertSubview:_navigationView belowSubview:self.navigationController.navigationBar];
[self.navigationController.navigationBar.superview insertSubview:_navigationViewBorder belowSubview:_navigationView];
Why not add the view to your viewController's view, and set up its constraints so that it sits just below the navigationBar, but above the tableView?
If it's only supposed to be there some of the time, you can animate its constraints to show/hide it.

Resources