Change view on button click - iphone

I am doing an iPhone project.
As part of it I want to know how to load a uiview to half portion of the screen when we click on a button.

You can use addSubview: from UIView Class

UIView *customView =[UIView alloc]initWithFrame:CGRectMake(10,100,200,200);//Give frame according to your view
customView.backgroundColor=[UIColor greenColor];
[Self.view addSubView: customView];

Related

Kal for iOS: there is a way to do not show the table view?

There is a way to do not show the UITableView in Kal, i.e., just the calendar (little squares) interface?
Thanks in advance!
You'll have to do this in code as Kal does not expose a way to do it natively. You'll have to edit the code in KalView.m.
Just comment out the below line in KalView.m addSubviewsToContentView: (line 179 in the GitHub repo I'm using).
[contentView addSubview:tableView];
This will result in a black box which fills the rest of the parent view. If you want a different color then you need to set a different color/background on the contentView or add your own UIView with the same frame as tableview. You could do something like this.
UIView *myView = [UIView alloc] initWithFrame:fullWidthAutomaticLayoutFrame;
myView.backgroundColor = [UIColor redColor];
[contentView addSubview:myView];
This will replace the tableView with a plane UIView with a red background.

How to force a UIButton to always be on top

I have a UIButton and a UIView that animates, sliding down when the UIButton is pressed. The problem is that the UIView obscures the UIButton. I'd like the UIButton to always remain on top.
I tried this in the viewDidLoad:
[self.view bringSubviewToFront:categoryBtn];
doesn't work though.
thanks for any help
After Add any UIView or any other view on your this mainview , after that just add your this line for brings as a supeview for button
[self.view bringSubviewToFront:categoryBtn];
For Example on button action event you add any view then use code like bellow..
-(IBAction)yourButton_Clicked:(id)sender
{
///Add some code or view here and then after write this line here see bellow line
[self.view bringSubviewToFront:categoryBtn];
}
The other answer is will work, but this is exactly the kind of thing "insertSubview: belowSubview" was added for.
[self.view insertSubview:myNewView belowSubview:_categoryBtn];
This allows you to add the new view below the button on the view stack instead of adding it above the button and then rearranging the views.

UIImageView allows button beneath it to be touched

Am developing an iPhone App. When the user taps a button, displaying a semi-transparent UIImageView on a page (i.e. covering the whole page). Now, since the UIImageView is semi-transparent, the buttons beneath it are seen. Clicking on the buttons (though the UIImageView is on top of it) is causing the button-click to be processed ; but I want to stop this. When the UIImageView is on th e top, only touching UIImageView (which calls a method) should work ; touching anything beneath it shouldn't.
Suppose yourImageView is your UIImageView above your button, do this,
[yourImageView setUserInteractionEnabled:YES];
You have following alternatives :
First make sure that UIImageView is on top of button. Bring if front by using bringSubviewToFront
You can disable userInteraction of UIImageView.
If you have any action associated with UIImageView , then you can prefer UIButton over there instead of UIImageView and set your image as a background of UIButton , and add action you required to perform with this button.
your_semi_transparent_ImageView.userInteractionEnabled = YES;
You can add a UIView on the click of a button and then add UIImageview as a subview of view. Then decrease the alpha of uiview.
It would be better if you will add your semi-transparent imageView not to self.view but to your application's window. like this:
AppDelegate *delegate = (Appdelegate *)[[UIApplication sharedApplication] delegate];
[delegate.window addSubview:yourImageView];
To remove yourImageView just use
[yourImageView removeFromSuperView];
Hide and disable the buttons when the ImageView is shown:
[button setEnabled:NO]; // If you want the button not to respond
[button setHidden:YES]; // If you want the button not to be seen

iOS5 Toolbar background image

I'm very new to iOS programming.
I'm trying to set the toolbar background to a custom image.
I'm also using storyboards.
How do I go about that?
Do I edit UIToolbar in the UI Kit framework? Do I need to change something in Storyboard?
Thanks,
You can use UIToolbar's built-in -setBackgroundImage:forToolbarPosition:barMetrics: method:
// portrait
[yourToolbar setBackgroundImage:[UIImage imageNamed:#"YourToolbarBkg-Portrait.png"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
// landscape
[yourToolbar setBackgroundImage:[UIImage imageNamed:#"YourToolbarBkg-Landscape.png"] forToolbarPosition:UIToolbarPositionAny barMetrics: UIBarMetricsLandscapePhone];
YourToolbarBkg-Portrait.png will be 320x44 bkg image for portrait mode
YourToolbarBkg-Landscape.png will be 480x32 bkg image for landscape mode.
UIToolbar inherits from UIView. This just worked for me:
[topBar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:BAR_BKG_IMG]] autorelease] atIndex:0];
UPDATED
topBar ------ is the outlet of the UIToolBar u are using
use this code where u are creating ur UIToolBar the class which implements the UIToolbar..
plus tell me y r u using Toolbar whats ur main purpose for it
Instead of editing UIToolBar, why not create a UIView of the same size and skin that however you would like? That would be easier if you are new.
Or if you want to override UIToolbar:
#implementation UIToolbar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: #"image.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
Had a right faff with background images for ToolBars and NavBars. I know setting the background image of a NavBar and ToolBar is generally a doddle. But, when you're presenting modal VC's, for some insane reason, where a NavBar is being added and you DO change the background image, it appears to double in size. Altering the height has strange results.
My issue was where I was using a NavController all over my App, but needed a modal view for one or two aspects of it. Simple enough. However, I needed either a NavBar or ToolBar type header with a Done button to pop the VC off the stack. Again, not an issue. But I needed the NavBar or ToolBar to look the same as everywhere else in my App.
I settled on a ToolBar, seeing as the VC was being presented modally. So, there my ToolBar sat, in typical Apple-blue. Nice, but not how the rest of my App looked, where a blackened image was being used for each NavBar. Using the iOS 5 appearance proxy, I altered the background image of the ToolBar. And this worked. But, unless I had the UIImage in exactly the proportions and size expected by the Tool Bar, I was in a pickle. The image simply did not look right at all. So, I decided to create a UIIMageView, where I could control the content mode, then insert a subview onto the toolBar.
Take a look at my code below.
UIImageView *toolbarImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:NAV_BAR_BACKGROUND]];
[toolbarImageView setFrame:[self.IBOToolBar bounds]];
[toolbarImageView setContentMode:UIViewContentModeScaleAspectFill];
[self.IBOToolBar insertSubview:toolbarImageView atIndex:1];
It's a bit of a fluff but I do hope this helps someone alter the image on their ToolBar.
NAV_BAR_BACKGROUND can be defined as follows:
#define NAV_BAR_BACKGROUND #"navBarBlackMattDarkSquare.png"

How to add UISlider into a bottom Toolbar?

Is there any way to add a UISlider into a Toolbar, I need to add some other objects also. I am creating a tabbar based application with tableview. And I don't want to hide the tabbar.
please check the figure.
Create your customUIView or customUIToolBar with frame size (0, 322, 320, 110)
Add two UIButtons (with right frame size) in it with addSubView property.
[self.customToolBar addSubView:button];
Add that green thing image as you have shown in your question as a subView.
Add a UISlider as a subview.
[self.view addSubView: customUIToolBar];