RectView doesn't move away from UIWindow - objective-c

I have the following simple code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
CGRect firstFrame = CGRectMake(300, 300, 150, 259); //CGRect is a struct not a simple object
ExampleView *view = [[ExampleView alloc] initWithFrame:firstFrame]; //crea la view
view.backgroundColor = [UIColor redColor]; //colora la view
[self.window addSubview:view];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
I'm working on XCode 5.1.1 but problem is that the red rect it is showed remains in the same position despite changing the values in CGRectMake. Do you have any idea why?
EDIT
This is the code
#import "ExampleView.h"
#implementation ExampleView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end

Related

Clean programmatically implementation of UI

I am trying to make a clean implementation of programmatically created UI.
I start off with my AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
MainViewController *mainVC = [[MainViewController alloc] init];
UINavigationController *navC = [[UINavigationController alloc] initWithRootViewController:mainVC];
self.window.rootViewController = navC;
[self.window makeKeyAndVisible];
return YES;
}
Then the MainViewController.m is a subclass of UIViewController implements the following:
- (void)loadView {
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
MenuView *contentView = [[MenuView alloc] initWithFrame:applicationFrame];
self.view = contentView;
}
And the custom UIView in MenuView.m implements the following
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
NSLog(#"init got called");
NSLog(#"frame size %f %f", self.frame.size.width, self.frame.size.height);
self.backgroundColor = [UIColor greenColor];
}
return self;
}
...
- (void)loadView {
NSLog(#"loadView got called");
UIButton *newButton = [[UIButton alloc] init];
newButton.titleLabel.text = #"New Button";
newButton.backgroundColor = [UIColor blueColor];
[self addSubview:newButton];
NSDictionary *views = NSDictionaryOfVariableBindings(newButton);
[newButton setTranslatesAutoresizingMaskIntoConstraints:NO];
NSDictionary *metrics = #{#"buttonWidth": #(150), #"buttonHeight": #(150)};
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-(100)-[newButton(buttonWidth)]"
options:0 metrics:metrics views:views]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:[newButton(buttonHeight)]-(100)-|"
options:0 metrics:metrics views:views]];
}
When I run this, the simulator shows me a green screen - but there is no button. The NSLogs in the init method fire and show a frame size of 320 x 548, but the loadView method does not get called.
What am I doing wrong?
Thanks
- (void)loadView;
is a method of UIViewController class, not UIView's.
So you need to setup its subviews inside init method where you already set background color.
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
// Initialization code
}
return self;
}

How to change the viewcontroller's background color from gray color to setting's color?

I'm trying to change the viewcontroller's color.
Someone let me know like following.
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
}
Here is AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[[ViewController alloc] init] autorelease];
} else {
self.viewController = [[[ViewController alloc] init] autorelease];
}
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
This is ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
But, result is that viewcontroller is black screen.
How to change to settings backgroundcolor?
try this
self.view.backgroundColor=[UIColor clearColor];

initWithFrame is filling the screen instead of creating a frame of the size I want

I'm new to Cocoa development and suspect that this is a noob issue.
I'm creating a simple background canvas with a bespoke UIView. I was expecting that I would be able to create this canvas to be slightly indented down from the main view so that I could add a toolbar at a later date. However, when I use initWithFrame (and any other init for that matter), the canvas is always created to be the size of the full screen rather than slightly smaller than the full screen. Even if I completely override the values for CGRect it doesn't make a difference. I've set a touchesBegan event and set the background colour to green to be able to determine success but all I get are a completely green screen and a touch event that works everywhere.
Please help - this is driving me mad and I can't find the answer anywhere on the web.
The relevant code is as follows (there actually isn't much more code than this in the whole app so far):
AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.objMainViewController = [[MainViewController alloc] init];
self.window.rootViewController = self.objMainViewController;
[self.window makeKeyAndVisible];
return YES;
}
MainViewController.h
#import <UIKit/UIKit.h>
#import "viewCanvas.h"
#interface MainViewController : UIViewController
#property (strong, nonatomic) viewCanvas *objViewCanvas;
-(id)init;
#end
MainViewController.m
#import "MainViewController.h"
#import "viewCanvas.h"
#implementation MainViewController
#synthesize objViewCanvas;
-(id)init
{
if (self = [super init]) {
CGRect frame = CGRectMake(100, 100, 100, 100);
objViewCanvas = [[viewCanvas alloc] initWithFrame:frame];
}
return self;
}
- (void)loadView {
self.view = objViewCanvas;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
viewCanvas.h
#import <UIKit/UIKit.h>
#import "viewCanvas.h"
#interface viewCanvas : UIView {
}
//Init
- (id) initWithFrame:(CGRect)frame;
//Events
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
#end
viewCanvas.m
#import "viewCanvas.h"
#implementation viewCanvas
//Standard inits
- (id) initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
[self setBackgroundColor:[UIColor greenColor]];
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Touches Began Canvas" message:#"Canvas Touches Began" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles: nil];
[alert show];
}
#end
I think by default the view of a view controller fills the whole screen, so you need to add a subview to that view that's your smaller viewCanvas object (BTW, you should conform to the naming rules and name your classes with capital letters, ViewCanvas). Try this in your MainViewController .m file:
-(id)init{
if (self = [super init]) {
CGRect frame = CGRectMake(100, 100, 100, 100);
objViewCanvas = [[ViewCanvas alloc] initWithFrame:frame];
self.contentView = [[UIView alloc] initWithFrame:CGRectMake(1, 1, 1, 1)]; //this frame doesn't matter
[self.contentView setBackgroundColor:[UIColor whiteColor]];
}
return self;
}
- (void)loadView {
self.view = self.contentView;
[self.view addSubview:objViewCanvas];
}

Can't return firstResponder status

Have set up my appDelegate to return a firstResponder status, but when I compile (it compiles fine), the console returns "Could not become the first responder" but doesn't tell why.
I have attached the code. Can you tell me what's wrong? Thanks in advance.
HypnosisterAppDelegate.m
#import "HypnosisterAppDelegate.h"
#import "HypnosisView.h"
#implementation HypnosisterAppDelegate
#synthesize window = _window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
HypnosisView *view = [[HypnosisView alloc] initWithFrame:[[self window]bounds]];
[[self window]addSubview:view];
BOOL success = [view becomeFirstResponder];
if (success) {
NSLog(#"HypnosisView became the first responder");
} else {
NSLog(#"Could not become the first responder");
}
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
-(BOOL)canBecomeFirstResponder
{
return YES;
}
You've implemented canBecomeFirstResponder on HypnosisterAppDelegate, which is not a UIResponder sub-class. You then send becomeFirstResponder to your custom view (HypnosisView).
HypnosisView (not the HypnosisterAppDelegate) needs to do this:
-(BOOL)canBecomeFirstResponder
{
return YES;
}

Refactoring after changing local variable into instance variable

Am having some trouble with my code after changing a local variable into an instance variable. To be specific, I have been having trouble with my code in my HypnosisterAppDelegate.m file after changing one line of code from:
HypnosisView *view = [[HypnosisView alloc]initWithFrame:screenRect];
to
view = [[HypnosisView alloc]initWithFrame:screenRect];
I have commented out the error messages in HypnosisterAppDelegate.m Can you tell me what's wrong? Thanks in advance.
HypnosisterAppDelegate.m
#import "HypnosisterAppDelegate.h"
#import "HypnosisView.h"
#implementation HypnosisterAppDelegate
#synthesize window = _window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
CGRect screenRect = [[self window]bounds];
// Create the UIScrollView to have the size of a window, matching its size
view = [[HypnosisView alloc]initWithFrame:screenRect];
[scrollView setMinimumZoomScale:1.0]; // Unknown receiver "scrollView"; should it be "UIScrollView"?
[scrollView setMaximumZoomScale:5.0]; // Unknown receiver "scrollView"; should it be "UIScrollView"?
//You will get a warning here, ignore it for now
[scrollView setDelegate:self]; // Unknown receiver "scrollView"; should it be "UIScrollView"?
[[self window] addSubview:scrollView]; // Unknown receiver "scrollView"; should it be "UIScrollView"?
//Create the HypnosisView with a frame that is twice the size of the screen
CGRect bigRect = screenRect;
HypnosisView *view = [[HypnosisView alloc]initWithFrame:screenRect];
// Add the HypnosisView as a subview of the scrollView instead of the window
[scrollView addSubview:view]; // Unknown receiver "scrollView"; should it be "UIScrollView"?
// Tell the scrollView how big its virtual world is
[scrollView
setContentSize:bigRect.size]; // Unknown receiver "scrollView"; should it be "UIScrollView"?
BOOL success = [view becomeFirstResponder];
if (success) {
NSLog(#"HypnosisView became the first responder");
} else {
NSLog(#"Could not become the first responder");
}
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return view;
}
#end
HypnosisView.h
#import <Foundation/Foundation.h>
#interface HypnosisView : UIView {
}
#property(nonatomic,strong)UIColor *circleColor;
#end
You don't have a variable called scrollView anywhere. I'd guess (especially from the comment // Create the UIScrollView to have the size of a window, matching its size, and also having gone through this book myself) that you meant to create a UIScrollView:
// not: view = [[HypnosisView alloc]initWithFrame:screenRect];
// but:
UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:screenRect];
and then add your HypnosisView later as a subview.