I'm new to x-code and I am trying to set the x,y position of a UILabel but I can't figure out why it is not working.
.h
#interface ViewController:UIViewController{
IBOutlet UILabel *badgeslabel;
}
#property (nonatomic,retain)IBOutlet UILabel *badgeslabel;
#end
.m
#implementation ViewController
#synthesize badgeslabel;
-(void)setBadge{
[badgeslabel setAlpha:.5];
[badgeslabel setCenter:CGPointMake(160,30)];
}
The setAlpha works, but the setCenter don't. Also, when I put the code in an IBAction the setCenter works but I don't know why.
I'm on xcode 5.0
Any help is appreciated, thank you.
As stated by Joel, if you have autolayout enabled then XCode won't allow you to manually set the x & y values of UIViews within a view controller because they are using constraints, attempting to do this will most likely break any constraints you have setup or XCode has added automatically and distort the position of any other UIViews you have in your View Controller.
If you don't want to use autolayout then simply disable it by going to the File inspector in interface builder (click your storyboard file), and untick "Use Auto Layout". This will revert to the old "Spring & Struts" way of laying out and allow you to set the position of your UIViews in code.
Related
I'm attempting to create a custom view that contains a play/pause button, and I'll attach any number of these to an NSWindow. I started by creating my own NSView and just drawing out the various pieces, then subclassing the play/pause button as an NSView (baby steps).
This all worked fine until I decided my button needed to extend NSButtonCell rather than an NSView. The following (from TimeSlipView.m) fails miserably, and I can't seem to figure out why:
playPauseButton = [[TimeSlipViewButton alloc] init];
[playPauseButton setButtonType:NSMomentaryPushInButton];
[self addSubview:playPauseButton];
I get a compile error and this warning for that last line: "Incompatible pointer types sending 'TimeSlipViewButton *__strong' to paremeter of type 'NSView *'".
I have a feeling I've misunderstood something very basic, and that for some reason I can't just pass addSubview: my NSButtonCell from within an NSView.
TimeSlipView.h
#import <Cocoa/Cocoa.h>
#import "TimeSlipViewButton.h"
#interface TimeSlipView : NSView {
TimeSlipViewButton *playPauseButton;
NSView *timerText;
NSView *clientText;
NSView *projectText;
NSView *taskText;
}
#end
TimeSlipViewButton.h
#import <Cocoa/Cocoa.h>
#interface TimeSlipViewButton : NSButtonCell
#end
A Cell Is no View and thus cannot be used as such! what you do doesn't work
You try exactly that when adding it as a subview
Cells are a (legacy) concept where views were too expensive.
The were/are used by some controls (like NSButton) to handle the actually drawing.
the Button CONTAINS a button cell
It ISNT a button cell, it IS a NSView
what you might wanna do is give a stock NSButton a specific ButtonCell that has custom drawing options. There are good tutorials out there for given existing NSButtons/NSSegmentedCells/NSTextFields custom NSCells
I'm just wondering about this extremely simple thing. How do you implement one, ONE, static image to your storyboard?
What i have tried so far is to have ViewController.h to
#property (strong, nonatomic) IBOutlet UIImageView *image;
and viewController.m to
_imageArray = #[#"hemla.jpg"]; //Logo for some stuff, added in resources.
image.image = [UIImage imageNamed:_imageArray[0]];
Where I either put "0" at the end, which results in nothing at all (But app is running), or "1" for a "Thread 1: Signal SIGABRT" error. (After compiling, when launching app in Simulator)
Is there a better way to implement images, or is it just me doing something wrong?
If you're using a storyboard, you don't need code at all. Just setup the imageView in the interface builder. You can set the image name there.
If you want to do it in the code, your code is right, but probably at the wrong place, e.g. the init method. The imageView has not been created at this point. You should use viewDidLoad: or any other point after this.
I am trying to make very simple element with new iOS 6 SDK with auto layout.
I have an ImageView and Embed it in ScrollView. (everything build with Interface Builder). The .png file is set and imageView mode is set to "Top Left".
Implementation:
#import "ImaginariumViewController.h"
#interface ImaginariumViewController ()
#property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
#property (weak, nonatomic) IBOutlet UIImageView *imageView;
#end
#implementation ImaginariumViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView.contentSize = self.imageView.image.size;
self.imageView.frame =
CGRectMake(0, 0, self.imageView.image.size.width, self.imageView.image.size.height);
}
#end
When I run the app, the image is not scrolled. Doing all the same with auto layout turned off (with struts and springs), I have working scrolling.
I guess the problem is with constraints. Could anybody help me, please?
I just encountered the same issue in a tutorial that I was updating. I attempted programmatically deleting constraints, cursing, and banging my head against the wall - no luck.
About 5 minutes ago, however, I tried something that had fixed another issue I encountered, and, ta da! UIScrollView is working again! The solution was to move the old code that sets the UIScrollView contentSize property into an implementation of viewDidAppear, rather than viewDidLoad:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.theScroller.contentSize=CGSizeMake(200.0,2000.0);
}
I hope this helps someone else encountering some of the headaches that have appeared with Auto Layout.
Autolayout can be very confusing at first. You actually don't want to set the contentSize of the scrollview anywhere. With a pure autolayout approach the scrollview sets its own content size. See the section on autolayout and UIScrollView in the iOS 6 release notes:
The constraints on the subviews of the scroll view must result in a
size to fill, which is then interpreted as the content size of the
scroll view. (This should not be confused with the
intrinsicContentSize method used for Auto Layout.)
Note that this means that the constraints on the subviews of the scrollview must set explicit widths and heights and not use widths that vary based on aspects of the scrollview.
The second error here is that you set the frame of the UIImageView to the size of the image. With autolayout this is also unnecessary. The UIImageView actually has an intrinsicContentSize which is the size of the underlying image. (To change this you should set constraints for width and height with a high priority) That means that with auto layout to place an image in a scrollview and have it scroll the correct code should be the following:
** nothing at all!!! **
But theres still something you need to watch out for that could cause you to have an image that appears not to scroll and the hint is in the aforelinked release notes:
Note that you can make a subview of the scroll view appear to float
(not scroll) over the other scrolling content by creating constraints
between the view and a view outside the scroll view’s subtree, such as
the scroll view’s superview.
i.e. if you set constraints in interface builder and constrain the image view to a view above the scrollview in the hierarchy it will affect how the view appears to scroll. Mad!
Happy Coding...
I'm very new in Mac OS programming. At the moment I'm trying to create simple measurement application which will have one window with the toolbar at the top and the appropriate view in the bottom. Clicking button in the toolbar should result in switching view below it - e.g. clicking on the "Connection" button will show with connection settings, "Measurements" will show current data from the device.
The problem is - I don't know how to handle swapping views, maybe in other words - something I know but not exactly...
I found similar discussion here: NSViewController and multiple subviews from a Nib but there is no answer how to create NSWindowController and how to assign it to the Main window. Because I guess it is necessary to create NSWindowController to be able to swapping views. If I'm wrong, please correct me.
So I'm creating new project (called Sample here) and there is SampleAppDelegate.h file, which looks like:
#interface SampleAppDelegate : NSObject <NSApplicationDelegate> {
#private
NSWindow *window;
}
#property (assign) IBOutlet NSWindow *window;
#end
There is window ivar, which holds the only one window, created from the MainMenu.xib (as I think).
So how should I create NSWindowController for the window from the SampleAppDelegate?
Should I just create my WindowController subclass and in the function
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
of the SampleAppDelegate like this:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
MyWindowController *wc = [[MyWindowController alloc] initWithWindow:self.window];
[wc showWindow:self];
self.myWindowController = wc;
[wc release];
}
I'll be very grateful for any hints and help.
Marcin
You shouldn't need an NSWindowController to do view swapping, NSWindowController used (I think) just when you need multiple toplevel windows.
You can just subclass NSViewController for each type of view that you want, put each view into a nib, and call -(NSView *)view when you need a view to put into the bottom part of the window. You should be able to just add it to the window like normal, or put it in an NSBox by using setContentView:view
For your two views you'd create MeasurmentsViewController and a ConnectionViewController. Then you'd create your views in MeasurementsView.nib and ConnectionView.nib, and use those nibs to initialise your view controllers.
Then in your main window, if you were to put an NSBox, if you wanted to put the MeasurementsView into it
NSView *measurementsView = [measurementsViewController view];
[boxAtBottomOfWindow setContentView:measurementsView];
and to put the ConnectionView into it
NSView *connectionView = [connectionViewController view];
[boxAtBottomOfWindow setContentView:connectionView];
I have a view controller that is instantiated in appDelegate and pushed onto a navigation controller. The view controller's view is created with a xib file. The xib file puts a UILabel on the view (among other things). Now I need to set the label's text property programatically but I don't see how to get a reference to it. How do I reference the label object?
I'm using xcode 3.2.5 and building an iPad app.
Aside from IBOutlets, you can also set a tag property on the label in the IB. Then, when you need it you can do:
UILabel *label = (UILabel *)[self.view viewWithTag:111];
111 of course being the tag you assigned to the label in IB.
You do this with what's called an "outlet". You define them in your controller, mark them clearly as IBOutlet and then connect them in Interface Builder to your file owner (or other delegate object created in IB).
For instance, in your FooController.m you might have this:
#interface FooController ()
#property (nonatomic, weak) IBOutlet UILabel* fooLabel;
#end
Then you would select your label, and either control drag from it to the file owner, or go to its connections tab, and drag from the + under referencing outlet, to the file owner and select the fooLabel.
UPDATE: Code sample changed to reflect modern way of handling this case.
[self.view viewWithTag:NUMBER_OF_TAG]; does the trick. But remember that if you want to access the view you must do it on the viewWillAppear or viewDidAppear events.