Is it a bug for pushViewController - ios

I used the code below to present vViewController1
#property (retain,nonatomic) ViewController1 *vViewController1;
...
push vViewController1 from rootViewController
[self.navigationController pushViewController:vViewController1 animated:NO];
Viewcontroller1's viewWillAppear
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
CGRect frame = CGRectMake(0, 20, 320, 480);
self.view.frame = frame; ///aaa
NSLog(#"%f:%f:%f:%f",
frame.origin.x,frame.origin.y,frame.size.width,frame.size.height);
}
it outputs
0.000000:20.000000:320.000000:480.000000
but the view y position is always at
0
rather than
20
it looks like it is always
CGRectMake(0, 0, 320, 480);
and
self.view.frame = frame; ///aaa
can not relocate the position of the view.
I have try to set the position using code when call pushViewController,
but there is no way to make it work correctly.
Just wonder if it is a bug for pushViewController
Your comment welcome

I think you have autolayout turned on. So you need to override viewDidLayoutSubviews method like this:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
self.view.frame = CGRectMake(0, 0, 320, 480);
NSLog(#"%#", NSStringFromCGRect(self.view.frame));
}
And it's better to use NSStringFromCGRect for NSLog, rather creating 4 outputs doubles.

Related

SKView drawRect not getting called

I am trying to get screen capture working on my iOS SpriteKit game using this code (by Aroth)
Basically I need to replace the SKView of my UIViewController with my own ScreenCaptureView (see the link). I changed the ScreenCaptureView in the example to derive from SKView instead of UIView.
I override loadView function in my game UIViewController class:
- (void)loadView
{
ScreenCaptureView *newView = [[ScreenCaptureView alloc] init];
newView.frame = CGRectMake(0, 0, 568, 320);//hard code for now
newView.clipsToBounds = NO;
self.view = newView;
[newView setNeedsDisplay];
NSLog(#"loadView %x", (int)newView);
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"viewDidLoad view %x %f x %f", (int)self.view ,self.view.frame.size.width, self.view.frame.size.height);
But its not working. The drawRect in ScreenCaptureView never gets called.
Any help is really appreciated.

layoutSubviews and animations

I am experiencing animation problems.
This is my current setup so far
I have initialized my objects (UIViews) in the
- (id)initWithFrame:(CGRect)frame method
I adjust and layer the frames of views as well as added subviews in the - (void)layoutSubviews method.
When I try to animate the views/subviews, nothing happens. Everything worked well until I moved the layering to the "initWithFrame" method, it works good.
Could somebody explain what the the cause could be and why the layered views in layoutSubviews can't be animated well?
I believe that it might be due to the hierarchy of calling methods, am I right in thinking that?
Code (subclass of UIView):
- (id)initWithFrame:(CGRect)frame
{
(...)
backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
backgroundView.backgroundColor = [UIColor whiteColor];
(...)
}
- (void)layoutSubviews
{
CGRect frame = CGRectZero;
(...)
frame = (CGRect){ 100, 100, 200, 200 };
[backgroundView setFrame:frame];
(...)
[self addSubview:backgroundView];
}
- (void)animate
{
[UIView animateWithDuration:1.0f animations:^{
backgroundView.frame = CGRectMake(100, 100, 300, 300);
} completion:^{BOOL f){
NSLog(#"w: %f h: %f", backgroundView.frame.size.width, backgroundView.frame.size.height);
}];
}
Tried to call "animate" from subview (by using [(MyClass *)self.superview animate]) as well as send notification.
Always log was
w: 200 h: 200

UIView animate alpha when view is offscreen doesn't work

I have a UIView which is located offscreen and I'm animating the frame so that the view slides in offscreen from the bottom and is visible. I'd like to simultaneously animate the alpha property of a UILabel on the view as well so it fades in. Unfortunately it appears I can't do the alpha animation because the view is offscreen and doesn't appear to take hold. It looks something like this:
nextCell.titleLabel.alpha = 0;
[UIView animateWithDuration:collapsedAnimationDuration animations:^{
CGRect newFrame = lastCell.frame;
newFrame.origin = CGPointMake(lastCell.frame.origin.x , lastCell.frame.origin.y + THREAD_CELL_HEIGHT);
nextCell.frame = newFrame;
nextCell.titleLabel.alpha = 1;
}];
Is it not possible to start animating the alpha of the subview because it's offscreen? If I position the view on screen and then try the animation it looks great but that's not the effect I'm going for. Thanks for your help.
Is this code executed in cellForRowAtIndexPath? If so, try moving it to tableView:willDisplayCell:forRowAtIndexPath:. The table view resets various properties of the cell before displaying it.
From the AppDelegate didFinishLaunching method:
self.myView = [[MyView alloc] initWithFrame:CGRectMake(320, 480, 400, 400)];
self.myView.titleLabel.text = #"test text";
self.myView.titleLabel.alpha = 0;
[UIView animateWithDuration:10.0 animations:^{
CGRect newFrame = self.myView.frame;
newFrame.origin = CGPointMake(0 , 0);
self.myView.frame = newFrame;
self.myView.titleLabel.alpha = 1;
}];
[self.viewController.view addSubview:self.myView];
MyView is just this:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self addSubview:self.titleLabel];
}
return self;
}
- (UILabel *)titleLabel
{
if (!_titleLabel) {
_titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
}
return _titleLabel;
}
I did no important changes to the code you presented and it worked fine. So assuming you're not doing what Tim mentioned (it won't work if you're doing it), we need more details to help you out.

Cannot resize UITableView xcode 4.5.1 IOS 5.0

First off, believe me when I say I have tried all of the suggestions. But I am willing to try them again, so feel free to comment/answer. Currently my code looks like:
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
CGRect frame = CGRectMake(tblView.frame.origin.x, tblView.frame.origin.y, tblView.frame.size.width, tblView.contentSize.height);
[tblView setFrame:frame];
[tblView reloadData];
CGRect contentRect = CGRectZero;
for (UIView *view in scrollView.subviews)
contentRect = CGRectUnion(contentRect, view.frame);
[scrollView setContentSize:contentRect.size];
scrollView.frame = CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);
}
My #interface is:
#interface VH_HotelsListViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
IBOutlet UIScrollView *scrollView;
IBOutlet UIImageView *imageView;
IBOutlet UITableView *tblView;
NSMutableArray *hotelArray;
}
I have tied my storyboard table view into the tblView above. My table view is populated at run time from my database, so I know there are no issues there.
As you can see from above, I am getting the content size of the table view and trying to adjust the frame to the same height. I am accurately getting the content size, though, because it resizes my scroll view to that height. It just does nothing to the table view, unfortunately. Though if I NSLog(#"Table View Height: %g", tblView.frame.size.height) just after setting it, I get the same number as for the content size Table View Height: 3166. So it's like it's working, but it's not actually expanding it.
Well, I got no answers, so I've updated my project. I've taken the UITableView out of my storyboard altogether and now just create it in code:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
tblView = [[UITableView alloc] initWithFrame:CGRectMake(0, 150, 320, 500) style:UITableViewStyleGrouped];
tblView.dataSource = self;
tblView.delegate = self;
[scrollView addSubview:tblView];
}
Then in viewDidAppear:
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
CGRect frame = CGRectMake(tblView.frame.origin.x, tblView.frame.origin.y, tblView.frame.size.width, tblView.contentSize.height);
tblView.frame = frame;
[tblView reloadData];
}
This seems to be working.

Providing autorotation support to a subview loaded programmatically - iPad

I have an iPad project structured as:
- AppDelegate
- MainWindow
- View Controller
-- View
The View Controllers .m file loads another view programmatically and positions it on the screen. This view is going to be slid in and out.
I do this:
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect viewRect = CGRectMake(0, 0, 0, 0);
CalculatorView *v = [[[CalculatorView alloc]
initWithFrame:viewRect] autorelease];
[self.view.window addSubview:v];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
v.view.frame = CGRectMake(0, 0, 460, 320);
[UIView commitAnimations];
}
The issue that I am having is that the subview I'm adding here doesnt' seem to have the correct orientation. The project supports ONLY landscape, and launches to landscape. The container view is fine, and it contains some buttons which are fine as well. However this programmatically loaded view is stuck in Portrait mode. I have provided the following auto-rotation code (In the loaded view's .m):
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
But it never gets called.
So, how can I get the programmatically added subview to load in landscape and NOT portrait mode?
TIA!
The UIView class does not receive orientation change messages.
(specially the shouldAutorotateToInterfaceOrientation method, which is a UIViewController Method)
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006926-CH3-SW23
You will have to manually add a method in your view to inform it that the orientation has changed and you should call this method in your controller shouldAutorotateToInterfaceOrientation method.
To do that you will have to create a reference to you view in your controller and take care of the memory yourself.
#interface MyController : UIViewController {
CalculatorView *_calculatorView;
}
#end
#implementation MyController
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect viewRect = CGRectMake(0, 0, 0, 0);
//inits _calculatorView without the autorelease. Will be released in the dealloc method
_calculatorView = [[CalculatorView alloc]
initWithFrame:viewRect];
[self.view.window addSubview:v];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
_calculatorView.view.frame = CGRectMake(0, 0, 460, 320);
[UIView commitAnimations];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
//calls custom interface orientation method
[_calculatorView MyInterfaceChangedCustomMethod:interfaceOrientation];
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
-(void) dealloc {
[_calculatorView release];
[super dealloc];
}
#end
EDIT: If you CalculatorView is simple and all you need is to change its frame properly after the device rotation, I think the best approach would be using you view's autoresizingMask similar to the following
_calculatorView = [[CalculatorView alloc]
initWithFrame:viewRect];
_calculatorView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

Resources