I've UIViewController and another UIView with some images and labels on it.
I wanna change orientation and it works well, I can change aqGridview frame when rotating device,
My code :
- (void)makePortraitOrientation {
[gridView setFrame:CGRectMake(0, 43, 768,961)]; //works like a charm
HeaderView* dsd = [[HeaderView alloc] initWithParentID:self]; // in this way there is no errors but nothing changes
[dsd.label setFrame:CGRectMake(0, 0, 0, 0)];
[dsd setNeedsDisplay];
[self.HeaderView.label setFrame:CGRectMake(0, 0, 0, 0)]; //error, no object HeaderView in UIViewController
[self.HeaderView.label CGRectMake]
}
Header file of UIView
#interface HeaderView : UIView{
id parentID;
UIButton *menu;
UIImageView *icon;
NSString *product;
THLabel *label;
NSMutableArray *backs;
}
The main problem is how can i change frames and places of images and
labels which is on another UIView, not in my UIViewController ?
All the views created programatically.
Based on your controls frame you need to adjust the position of our controls while rotating the device.
Access controls which is in our view controller like this. [self.button setFrame:];
Access the sub view controls frame like this: [self.subviewname.control setFrame:];
Based on the orientation you need to handle the frames. You can use auto re-sizing also.
Update:
Make sure whether your sub view is added properly in your view controller like this :
[self.view addSubView:self.mysubView];
Increase the score of your sub view(HeaderView) as well.
Related
I have taken UIView as subview in View Controller
#property(nonatomic,retain) IBOutlet UIView *subview;
then I am adding custom UIView class in subview like this ,
- (void)showdetail
{
CGRect frameToset = CGRectMake(0, 0, self.subview.frame.size.width,self.subview.frame.size.height) ;
customUIViewPaging *demoView = [[customUIViewPaging alloc] initWithFrame:frameToset];
... // so on
[self.subview addSubview:demoView];
}
In portrait mode it works perfectly but in landscape I am updating the frame like this,
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[self showdetail];
}
then after it works perfectly in landscape and portrait then I am pushing a another view from this view and landscaping Pushed view after back coming in this view then frame size not getting perfectly (Subview frame not updating the frames while orientation when coming back from another view)
I have tried to put needsUpdateConstraints but it seems not working
[self.subview needsUpdateConstraints];
Check this example
I am not going to Autolayout direction as you want correct frame when view appears ... If you want correct width than call it -(void)viewDidLayoutSubviews{..}
-(void)viewDidLayoutSubviews{
NSLog(#"%f",self.subview.frame.size.width); //here you get correct width
}
now the other problem is you are adding subviews everytime when screen rotates or this method calls .. so better to prevent that by removing previous view...
Here I have created Navigation bar with height 50 and width as view size.Now, I want to add a subview in the navigation bar with same navigation bar size.How to set the frame size for subview?
ViewController.m
-(void)ViewDidLoad
{
UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
[self.view addSubview:navbar];
}
Firstly, your code won't respond to the orientation changes (portrait / landscape handling) nor support split screen. To fix it, instead of setting frame in viewDidLoad (where btw you do not call super!) write:
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
[self.navBar setFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
}
Then as it is a different view, you should create a subclass of your view. The code will work in a way as #raghu wrote, but it won't be easily maintanable and will be of low quality. Separate of concerns!
Inside your UIView subclass you should implement init methods and layoutSubviews where you should set a proper frame.
A uiviewcontroller has two UIViews and one tableview added in stroyboard ib and connected with IBoutlet
when user click swipe in top uiview, I call following method to change frames of all subview IBoutlet elements. That works.
-(void)hideTopViews
{
[UIView animateWithDuration:0.25 animations:^{
[_overallHealth setHidden:YES];
_overallHealth.frame = CGRectMake(0, 0,self.view.frame.size.width,0);
[_healthBar setHidden:YES];
_sortView.frame = CGRectMake(0, 45,self.view.frame.size.width,_sortView.frame.size.height);
_portfoList.frame = CGRectMake(0, _sortView.frame.origin.x+_sortView.bounds.size.height+50,self.view.frame.size.width,self.view.frame.size.height);
_sortButton.frame = CGRectMake(90, 45,_sortButton.frame.size.width,_sortButton.frame.size.height);
[self.view bringSubviewToFront:_sortButton];
self.navigationItem.leftBarButtonItems=nil;
}];
}
However when I add another subview programmatically to self.view all subviews goes back to their original positions like in storyboard.
-(void)showPortfolioDetailsScreen
{
PortfolioDetails *portView=[[PortfolioDetails alloc] initWithFrame:CGRectMake(300,200,200,200)];
portView.backgroundColor=[UIColor redColor];
[self.view addSubview:portView];
}
How can I fix this?
#tanzolone is on right track, if you are using only portrait mode and want your storyboard to stop changes your subviews to original frames go to your storyboard and uncheck Use Autolayout
so you can use the old struts-and-springs model.
here is a detailed tutorial of autolayouts though
http://www.raywenderlich.com/20881/beginning-auto-layout-part-1-of-2
I have a webview and when you click a link on a webpage, I want the screen to show an image for the duration of the time that the page is laoding (it sounds weird, but it's for a good reason).
I'm new to XCode and have done a few tutorials, but it's still murky to me how to make things happen in XCode (and objective C in general).
It seems that the process should be this - in the webViewDidStartLoad method, I call a class, which is a UIImageView. In that class is the image. And in webViewDidFinishLoad, I then either remove the image, or lower it on the stack.
I just am confused as to all the things you need to do to make this happen. Here's what I have so far:
A class called loadingView which has a method in it called showLoadingScreen. In this method I have the code:
UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
[view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"Icon-76.png"]]];
Then I go to the storyboard and insert a new UIView over the webView, and give it the class loadingView. And then I control clicked from this UIView to the ViewController.h, and dropped it in the view controller, creating the code:
#property (strong, nonatomic) IBOutlet UILabel *loadingPageView;
and imported loadingView.h.
Is this correct? If yes, then I need to put this method into webViewDidStartLoad, but I can't figure out what code to put there. It seems I to do something like:
[self.view addSubview:imageView];
But I don't know how to customize this to my code.
If you want a view to cover the screen, use:
[[[UIApplication sharedApplication] keyWindow] addSubview:myImageView];
This will add your UIImageView to your key UIWindow, which will show over any UIViewController. Otherwise, add your UIImageView as a subview of your UIWebView. If you want to guarantee that your UIImageView is at the top of the view stack, call:
[self.webView bringSubviewToFront:myImageView];
Either way, maintain your UIImageView as a global property/ivar so you can later call:
[myImageView removeFromSuperview];
Step 1: Declare an imageview property somewhere:
#property (strong, nonatomic) UIImageView * coverImageView;
Step 2: Show Image view in webViewDidStartLoad
- (void) webViewDidStartLoad:(UIWebView *)webView {
if (!_coverImageView) _coverImageView = [[UIImageView alloc]init];
_coverImageView.frame = self.view.bounds;
_coverImageView.image = [UIImage imageNamed:#"Icon-76.png"];
[self.view addSubview:_coverImageView];
}
Step 3: Remove Image View in webViewDidFinishLoad
- (void) webViewDidFinishLoad:(UIWebView *)webView {
[_coverImageView removeFromSuperview];
}
I already have the code to animate UIPickerView to act like iOS keyboard, but the problem is when it's loaded for the first time, the UIPickerView is already shows up. I want to hide it first outside UIView area, until UIButton tapped. so it will animate just like iOS keyboard.
here's the picture to illustrate what I mean :
how can it be done? thank you.
Just set the frame of pickerView accordingly while initiating.
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(100, 900, 200, 200)];
if you want pickerView to appear from bottom then increase the y co-ordinate and if you want pickerView to appear from right then increase the x co-ordinate value
if pickerView is on the UIView which is again on some UIViewController then set the ClipsToBounds to YES.
[yourView setClipsToBounds:TRUE];
Hope this will help you.
Sorry i cant comment so answering from this I think you are already placed an UIpicker view in your view concept is same as #xman said. If you didnt place picker view place it
then in .h create property for picker view
#interface ViewController : UIViewController
{
IBOutlet UIPickerView *statepicker;
}
#property (weak, nonatomic) IBOutlet UIView *statepickerview;
in .m first synthesize
#implementation ViewController
#synthesize statepickerview;
in viewdidLoad initially set frame of picker view out of view example
- (void)viewDidLoad
{
statepickerview.frame = CGRectMake(-9, 506, 367, 263);
[super viewDidLoad];
}
then in action call the frame to visible postion
- (IBAction)ButtonPress:(id)sender {
statepickerview.frame = CGRectMake(0, 250,321, 260);
}
when you press button if it is not visible then try to change the co-ordinates
If this not works properly check this link create view and place the picker view inside the custom view and change the frame size of the view
see this link surely it will help
You can set pickerview's y position with your window's height. Like this :
[self.view addSubview:pickerView];
pickerView.frame = CGRectMake(0, appDelegate.window.frame.size.height,pickerView.frame.size.width,pickerView.frame.size.height)
You have to add pickerview in your view and then set frame of it.
I found answer... case closed... it's all because of 'AutoLayout' is enabled on my XCode Project... when it's disabled, everything works as I expected.