I am using Cocoa and I have a NSViewController with the dimensions set. However, I want to programmatically change the dimensions based on an if/else statement. Basically "if this condition is met - the NSViewController becomes this size." Is there a way to change the dimensions of the NSViewController Frame or a way to do it with the initial Window Controller? I couldn't find anyone with the current correct syntax to do something like this.
Related
I am attempting to change the coordinates of an NSButton that is contained within a parent NSView and something is clearly not working, because the button position does not change. Both elements are defined in a nib file and the parent view has animation applied to it using CoreAnimation.
I have tried the following.
button.frame.origin.x = 500
and...
var frame:CGRect = button.frame
frame.origin.x = 500
button.frame = frame
Even with the animations disabled, I can not seem to dynamically position the subview. Is there some feature that prevents children views from being positioned programmatically?
Please note that I am using Swift with XCode 6.3.1.
I'm guessing you're using AutoLayout constraints, given you're using the latest tools.
If so, setting a subview's frame directly won't work the way you're expecting (if it does anything at all, it'll cause strange drawing glitches / flashing when mixed with animation). You have to create outlets for your layout constraints and modify them.
If you're not using AutoLayout, I suggest having a look at your button outlet to make sure it's actually connected (ie, you're not talking to nil). Even if the outlet is connected, make sure it's not nil at runtime - you may be trying to talk to the button before the nib is loaded and the outlet / action connections are restored.
what I want to do is,
I am using UICollectionView to show my stuff.
What I want is, the stuff that I want to show is coming from server,
It may be in single line or some may be multiple.
I wanted to show the cell frame of UICollectionView differently.
Means If data is big then bigger frame of UICollectionViewcell
and if data is less then smaller frame of UICollectionViewcell.
I tried a lot,
but I am succeed only in having fixed size of frame for UICollectionViewcell
How to achieve different frame for each cell according to need.
Is it possible....?
#note :- I am not using any xib.
All is done programatically.
It seem like you are getting UICollectionView wrongly.
Let me say in short You are not suppose to set frame but you need to implement your own UICollectionViewLayout.
By default UICollectionView use UICollectionViewFlowLayout. Which add cell one by one.
But UICollectionView is very power full UI component than that. To open its true power you have to understand UICollectionViewLayout.
Here are some good example of that :
RFQUILTLAYOUT
CHTCollectionViewWaterfallLayout
CCHexagonFlowLayout
MSCollectionViewCalendarLayout
If you want to learn how to create your ow than have a look at following link:
Creating Custom Layouts
Custom Layouts: A Worked Example
Implementing UICollectionViewLayout
UICollectionView custom layout tutorial
A Springboard-Like Layout With the UICollectionView Class
If you need to know overview of how UICollectionView work than read : NSHipster:UICollection​View
One in depth look in Custom Collection View Layouts : objc : Custom Collection View Layouts
I hope I get u correctly. And this will lead you to right direction.
Is there a simple way to add a simple rectangle to a Custom View without using a custom NSView subclass for it? Something along the lines of:
Assign an IBOutlet (let's call it colorWheelView) of NSView type to the CustomView
In my NSViewController's initWithNibName use it to change draw the rectangle:
// pseudocode
self.colorWheelView.addRectangle(myRectangle);
self.redraw()
The only way I've seen it done (on this site, and in my book Cocoa Programming for Mac OSX, pp. 241) is by making a custom class for the Custom View and modifying its drawRect method... Is this really the only way to accomplish this?
Edit: not sure why formatting is not being rendered correctly. I'm trying to fix it.
It really isn't all that hard to roll your own..
Just add an NSArray property to your NSView subclass, then in your drawRect method draw them either manually or using one of the NSRectFillList* methods provided by AppKit already.
(Beware: those take a plain C array, not an NSArray).
You wouldn't want to manually trigger the redraw from outside the view as in your sample code, though. To keep things consistent your addRectangle would trigger a redraw of the view itself e.g. by calling setNeedsDisplay:.
I want an NSTextField to get bigger as the user types, and smaller as backspace is pressed etc.
The only thing I've found is [textField sizeToFit] but that doesn't run dynamically, it's just a one off resizing. Is there a property or something I need to set?
p.s. I only need it to resize horizontally (single line text).
What Richard said, except you need to know by how much to resize your textfield. To get this value, use the the layout manager of the NSTextField and call something like boundingRectForGlyphRange:inTextContainer: on it with a range of all glyphs. This should tell you the dimensions of the rectangle bounding all of your text. Take these dimensions and set them to the width and height of your NSTextField content.
Set a delegate on the NSTextField which implements
- (void)controlTextDidChange:(NSNotification *)aNotification
Every time the delegate gets called resize the control per your idea above.
I just created a new Xcode project. In the AppControl class Header file I have the following objects defined (and some other ones, too):
IBOutlet NSImageView *inputImageView;
IBOutlet NSImageView *outputImageView;
IBOutlet NSTextField *myNoiseLevel;
IBOutlet CGFloat *mySharpness;
After putting the basic code into the .h and .m files, I then went into Interface Builder and created my UI. I was able to bind the two NSImageView controls in IB to the corresponding NSImageView objects listed above. And I was able to bind a couple of other objects/controls, also. But I am NOT able to bind the last two items listed (myNoiseLevel and mySharpness) to the NSSlider controls I have on the application main window. I'm not sure why. I know this kind of thing is probably hard to diagnose, because it is not "strictly code related," but if there is something "tricky" about binding sliders please let me know what the main "suspects" are that I should check.
This is my first attempt to use a slider control through IB. I have a book (Cocoa programming for Mac OS X, 3rd ed., by A. Hillegass) that I am using to learn about the basic way to do this stuff. And he has a slider example in there. But his slider example is "continuous" and it uses key path binding. I think this is overkill for what I want/need to do -- I just want to pull the value from the slider when another button is pushed (no need for "continuous" update). So I am trying to directly bind the "outlets" listed when I right-click on my App Control object (one for each of those items shown above), to the slider controls on my window. But when I cntl-drag from the AppControl outlet up to the corresponding slider, the slider will not "accept" the arrow I'm dragging.
Does this make sense? Any idea what I'm doing wrong and/or what I need to do to make the binding work? I have tried saving / building / closing & reopening IB and Xcode -- all to make sure IB has the latest version of everything. Still no luck, though.
One last thing ... What I really need are CGFloat numbers, from the slider. Can I simply declare the Outlet as CGFloat type ... or do I need to define it as NSTextField (or something else), and then convert it to Float in my program? You can see in the IBOutlets I pasted above, that I was trying different data types for the outlets (trying to see if my defining them as CGFloat was somehow preventing the bindings).
Make the outlet an NSSlider*. You should then be able to connect to it. When you need the value (eg, in response to the button press you mention) call [yourSliderOutletName doubleValue].
More generally: an IBOutlet is an ivar that can be filled in with a pointer to the actual object awoken from a NIB file. As such, it needs to be of an appropriate type to hold that pointer -- the object's actual class, or one of its superclasses or protocols, or (least informatively) id. You can't just arbitrarily connect an object to any old variable, like your CGFloat. There's no implicit conversion -- how is the system supposed to know what you want?