I want to change the existing view in a UIViewController to a new view. The new view contains the old view and a little banner view.
Doing this fairly simple change leaves me with a black view.
My code looks like this
UIView *existingView = self.view;
UIView *newView = [[UIView alloc] initWithFrame:existingView.frame];
UIView *bannerView = [[UIView alloc] initWithFrame:CGRectMake(0, (self.view.frame.size.height - 50), 320, 50)];
CGRect existingViewFrame = existingView.frame;
existingViewFrame.size.height -= 50;
existingView.frame = existingViewFrame;
[newView addSubview:existingView];
[newView addSubview:bannerView];
self.view = newView;
However when switch Tabs and come back to the view which changed the view is shown just like I want. I guess I need to set a flag or something to tell the controller to redraw it's (new) view.
Edit
I wrote an simple example for this problem. You can find it on GitHub: https://github.com/Oemera/ChangeView
You did not say where you do this. It may be that you need to save the original view's super view, then add the new view to that views subViews array. I'm betting that is the problem.
Related
I have three views : topView, middleView, bottomView.
When I clicked the button in the topView, I want to insert alpha view between topView and middleView.
but middleView and bottomView is already located by calling method [self.view addsubView:] at viewDidLoad.
So I have to change middleView and bottomView below the alpha view...
So I called method 'setFrame', but already added views didn't move to where i want to put them.
So I have to call method 'removeFromSuperView' but there is a big problem that middleView and bottomView are the master views that has multiple subviews into them...
I don't know how to solve this problem, please help me!
! middleView and bottomView, they just need to go down , moving issue is simple.
****UPDATE
My code about the issue is below :
authCodeView is alphaView
authCodeView = [[UIView alloc] initWithFrame:CGRectMake(0, topView.frame.size.height + 8, [UIScreen mainScreen].bounds.size.width, topView.frame.size.height)];
[authCodeView setBackgroundColor:[UIColor whiteColor]];
authCodeField = [[UITextField alloc] initWithFrame:CGRectMake(30, 0, authCodeView.frame.size.width - 30, authCodeView.frame.size.height)];
authCodeField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"인증번호 입력" attributes:#{NSForegroundColorAttributeName:tempGray}];
[authCodeView addSubview:authCodeField];
NSLog(#"%f",authCodeView.frame.origin.y+authCodeView.frame.size.height + 8);
[middleView setFrame:CGRectMake(100, authCodeView.frame.origin.y+authCodeView.frame.size.height + 8, [UIScreen mainScreen].bounds.size.width, 308.0f)];
[bottomView setFrame:CGRectMake(0, middleView.frame.origin.y+middleView.frame.size.height+8, [UIScreen mainScreen].bounds.size.width, bottomView.frame.size.height)];
[self.view addSubview:authCodeView];
[self.view addSubview:middleView];
[self.view addSubview:bottomView];
So I called method 'setFrame', but already added views didn't move to where i want to put them.
Then you passed the wrong rectangle to -setFrame:. That method changes the frame of the view, i.e. it's location and size in the coordinate system of its superview.
So I have to call method 'removeFromSuperView' but there is a big problem that middleView and bottomView are the master views that has multiple subviews
You don't need to remove a view just to change its position. You'd only need to remove it if you want to add it to some other view instead, or if you no longer want it in the superview at all. That said, it's not a problem if a view like middleView has subviews -- those will go along with middleView if you remove it from its superview, or if you change middleView's position, etc.
I don't know how to solve this problem, please help me!
When asking for help, you really need to post code that reproduces the problem you're trying to solve.
I implemented a horizontal table view and it looks like this
The category bar, Dining Shopping something, is a horizontal table view and here is the implementation code.
LogInfo(#"Show Category Table start");
// add categories to be subview controller
self.categoriesTable = [[UITableView alloc] init];
self.categoriesTable.transform = CGAffineTransformMakeRotation(-M_PI * 0.5);
[self.categoriesTable setFrame:CGRectMake(0, 64, SCREENWIDTH, 44)];
self.categoriesTable.showsVerticalScrollIndicator = NO;
self.categoriesTable.showsHorizontalScrollIndicator = NO;
self.categoriesTable.pagingEnabled = YES;
self.categoriesTable.delegate = self;
self.categoriesTable.dataSource = self;
self.categoriesTable.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.superView addSubview:self.categoriesTable];
LogInfo(#"Show Category Table Finished");
self.categoriesTable.backgroundColor= [UIColor redColor];
It works as expect but if I change view, for example, I click any button to go to other view and go back to this view. The tableview looks like this
This problem also happpens even if I disable the bounce effect of the table view. Does anyone know how to solve this problem? Thanks!
Rather than applying a transform to the table to make it horizontal, use a collectionView with a horizontal layout.
Edit: If you want to continue using your current setup, try disabling automaticallyAdjustsScrollViewInsets on your view controller.
self.automaticallyAdjustsScrollViewInsets = NO
Edit 2: If you're curious, since iOS 7 every view controller looks at its topmost/first scroll view and adjusts its contentInsets to compensate for the navigation bar transparency which is enabled by default. In this case of course, such behaviour isn't desired at all.
I want to add a view called anotherView on top of self.view. But I don't want anotherView to be a subview. Is this possible?
I want anotherView to be at the top of the page and push the contents of self.view down accordingly (without having to change the y or height values.
I have the following and it doesn't work:
UIView *anotherView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.view.frame.size.width, 80)];
anotherView.backgroundColor = [UIColor yellowColor];
[self.view insertSubview:anotherView aboveSubview:self.view];
The box is simply on top of the existing view.
Any visible view is subview of some other view or window.
But you can add anotherView to
self.view.superview
or even
self.view.window
or create new UIWindow object and add your subview to this window
Have you tried to present anotherView modally? That way you can make it to appear on top of everything being a completly new view controller. Ex:
[self presentModalViewController:anotherView animated:YES];
assuming of course anotherView is a subclass of UIViewController and has its own View property. Because if anotherView is just subclass of UIView then you can't present it without being a subview of the current View Controller.
EDIT:
Oh yes, you can just pass a nil to the completion block:
[self presentViewController:anotherView animated:YES completion:nil];
I have written some code which applies and image to a view for a UIViewController. The code is supposed to be iPhone screensize independent in as far as the difference in height between the iPhone 4 and 5.
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
self.view.clipsToBounds = YES;
UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:#"myimage.png"] drawInRect:self.view.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIView *imageView = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height)];
imageView.backgroundColor = [UIColor colorWithPatternImage:image];
[self.view addSubview: imageView];
image = nil;
imageView = nil;
I found that when I add this code to the viewDidLoad method, the code failed to detect the different window size. However when I place it in viewWillAppear, the code does correct work with both screen sizes. I don't understand why.
Does any one know why this would happen ? I would like to understand it.
thanks
This happens because at the time the view is loaded, its' content hasn't necessarily been laid out and the size isn't known. This is especially true when using the autolayout system. The basic steps are,
The view is loaded
The view is laid out by the system using the constraints you give in the storyboard or code
The view appears
So the most appropriate place to put this appears to be viewDidLayoutSubviews. At that point the view and its subviews have been laid out, and the sizes are there. But putting it in viewWillAppear (or viewDidAppear, for that matter) will work, albeit will be less correct.
viewDidLoad method is called when the view controller's root view just has been created. At this point it is not added to the window hierarchy and auto layout has not been done. That is why you see this behavior.
This being said why draw the image in the graphic context? Just use UIImageView to display the image.
What you are doing is almost correct. As others have stated, in viewDidLoad the final size of the view controller's view is not set yet. The proper solution is to set the subview's autoresizingMask properly.
UIView *imageView = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height)];
imageView.backgroundColor = [UIColor colorWithPatternImage:image];
imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self.view addSubview: imageView];
This assumes you want the image view to fill the view controller's view.
Another solution is to update the frames of the subviews in the viewWillLayoutSubviews method.
I have a subview in my navigation bar. I try to add it by this way:
UIView *customView =
[[UIView alloc] initWithFrame:
CGRectMake(0, 0, image.size.width + label.frame.size.width, 44)];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[customView addSubview:imageView];
[customView addSubview:label];
[self.navigationController.navigationBar addSubview:customView];
However, when I try to push the navigation bar, the customView stays in the place, not animating following the sliding navigation bar. How can I achieve the animated subview? Is it even possible? Thanks!
you should not add subview in that way
you have tospecify your view location in the left , right or title view
self.navigationItem.titleView = YOURVIEW;
or choose another location left or right items in this way the the title view will added to the current view if you want to remove it just set it to nil in the place you want and reload it subviews again,
self.navigationItem.titleView = nil;
As you are using
[self.navigationController.navigationBar addSubview:customView];
that means the navigation bar you have create is in App delegate and is common for all the viewControllers in your project,that is why once you add a view on to it you see it on every view you have. Remove your sub-view in
-(void)viewWillDisappear:(BOOL)animated{
[Your subview remove fromSuperView];
[super viewWillDisappear:YES];
}
and add that subview in
-(void)viewWillAppear:(BOOL)animated{
[self.navigationController.navigationBar addSubview:Your subview];
[super viewWillAppear:YES];
}
this will add the subview in that particular view only and remove it as soon as that view is popped or pushed.The code given is not correct to the syntax please give a check on that.