NSView Frame Reverting after software installation

So, I have a view that I set to IB, and I need to change the frame programmatically. For some reason, the frame continues to return to its IB location after I installed it. I subclassed NSView and registered the frame in the method -setFrame:(NSRect)frameRect, and it looks like it -setFrame:gets called twice - once when I set it (where it registers new values) and once when it returns (where it registers IB values). I cannot distinguish the root of the problem because in some situations (for example, if I have an NSButton dedicated to setting or setting the frame timer), it works fine, but if I have a call -setFrame:to -line with my other code, it always returns .

Edit:

This is a simple example that shows the problem (the original frame in IB is {{20, 118}, {48, 48}}):

AppDelegate.m:

#import "AppDelegate.h"

@implementation AppDelegate

- (void)awakeFromNib{
    [self.button setFrame:NSMakeRect(50, 10, 100, 100)];
}

@end

Magazine

2014-02-18 18:01:40.206 WHS-ChangingFrameTest[15210:303] Frame: {{50, 10}, {100, 100}}
2014-02-18 18:01:41.223 WHS-ChangingFrameTest[15210:303] Frame: {{20, 118}, {48, 48}}

Edit # 2:

Calling Stack from when I edit a frame (from the original application):

0   MyApp                 0x000000010000203c -[FrameLogProgressIndicator setFrame:] + 284
1   MyApp                 0x000000010001c994 -[SubjectViewController updateTableViewHeight] + 1284
2   MyApp                 0x000000010001c468 -[SubjectViewController updateUI] + 4664
3   MyApp                 0x0000000100012f2f -[TabMenuViewController updateDisplayingBlock:] + 975
4   MyApp                 0x0000000100010c59 -[TabMenuViewController switchBlockFromDaySchedulePopover:] + 873
5   AppKit                              0x00007fff82eea959 -[NSApplication sendAction:to:from:] + 342
6   AppKit                              0x00007fff82eea7b7 -[NSControl sendAction:to:] + 85
7   AppKit                              0x00007fff82eea6eb -[NSCell _sendActionFrom:] + 138
8   AppKit                              0x00007fff82ee8bd3 -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 1855
9   AppKit                              0x00007fff82ee8421 -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 504
10  AppKit                              0x00007fff82ee7b9c -[NSControl mouseDown:] + 820
11  AppKit                              0x00007fff82edf50e -[NSWindow sendEvent:] + 6853
12  AppKit                              0x00007fff82edb644 -[NSApplication sendEvent:] + 5761
13  AppKit                              0x00007fff82df121a -[NSApplication run] + 636
14  AppKit                              0x00007fff82d95bd6 NSApplicationMain + 869
15  MyApp                 0x00000001000020a2 main + 34
16  libdyld.dylib                       0x00007fff8152a7e1 start + 0
17  ???                                 0x0000000000000003 0x0 + 3
)

Calling the stack from the moment the frame returns:

0   MyApp                 0x000000010000203c -[FrameLogProgressIndicator setFrame:] + 284
1   AppKit                              0x00007fff82e21e77 -[NSView resizeWithOldSuperviewSize:] + 659
2   AppKit                              0x00007fff82e21307 -[NSView resizeSubviewsWithOldSize:] + 318
3   AppKit                              0x00007fff82f08399 NSViewLevelLayout + 44
4   AppKit                              0x00007fff82f07e65 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 112
5   CoreFoundation                      0x00007fff84b524a6 __NSArrayEnumerate + 582
6   AppKit                              0x00007fff82f07fc6 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 465
7   CoreFoundation                      0x00007fff84b524a6 __NSArrayEnumerate + 582
8   AppKit                              0x00007fff82f07fc6 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 465
9   CoreFoundation                      0x00007fff84b524a6 __NSArrayEnumerate + 582
10  AppKit                              0x00007fff82f07fc6 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 465
11  CoreFoundation                      0x00007fff84b524a6 __NSArrayEnumerate + 582
12  AppKit                              0x00007fff82f07fc6 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 465
13  AppKit                              0x00007fff82f07cfe -[NSView layoutSubtreeIfNeeded] + 615
14  AppKit                              0x00007fff82f034ac -[NSWindow(NSConstraintBasedLayout) layoutIfNeeded] + 201
15  AppKit                              0x00007fff82dfd0a8 _handleWindowNeedsDisplayOrLayoutOrUpdateConstraints + 446
16  AppKit                              0x00007fff833c8901 __83-[NSWindow _postWindowNeedsDisplayOrLayoutOrUpdateConstraintsUnlessPostingDisabled]_block_invoke_01208 + 46
17  CoreFoundation                      0x00007fff84b20417 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
18  CoreFoundation                      0x00007fff84b20381 __CFRunLoopDoObservers + 369
19  CoreFoundation                      0x00007fff84afb7b8 __CFRunLoopRun + 728
20  CoreFoundation                      0x00007fff84afb0e2 CFRunLoopRunSpecific + 290
21  HIToolbox                           0x00007fff8231aeb4 RunCurrentEventLoopInMode + 209
22  HIToolbox                           0x00007fff8231ab94 ReceiveNextEventCommon + 166
23  HIToolbox                           0x00007fff8231aae3 BlockUntilNextEventMatchingListInMode + 62
24  AppKit                              0x00007fff82dfa533 _DPSNextEvent + 685
25  AppKit                              0x00007fff82df9df2 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 128
26  AppKit                              0x00007fff82df11a3 -[NSApplication run] + 517
27  AppKit                              0x00007fff82d95bd6 NSApplicationMain + 869
28  MyApp                 0x00000001000020a2 main + 34
29  libdyld.dylib                       0x00007fff8152a7e1 start + 0
30  ???                                 0x0000000000000003 0x0 + 3
)

Let me know if I need to publish code from the methods used on the stack so that this is useful. (sorry, I've never done this stuff before)

+3
source share
3 answers

I managed to stop the resizing of the views by subclassing and redefining them -resizeWithOldSuperviewSize:, just to do nothing, for example:

- (void)resizeWithOldSuperviewSize:(NSSize)oldSize {};
+1
source

resizeWithOldSuperviewSize: , NSView. NSView , , . :

[<id> setAutoresizingMask:NSViewNotSizable];
[<id> setTranslatesAutoresizingMaskIntoConstraints:YES];

<id> NSView, .. self.button. , , , . AppDelegate.m :

#import "AppDelegate.h"

@implementation AppDelegate

- (void)awakeFromNib{
    [self.button setAutoresizingMask:NSViewNotSizable];
    [self.button setTranslatesAutoresizingMaskIntoConstraints:YES];
    [self.button setFrame:NSMakeRect(50, 10, 100, 100)];
}

@end

Update: NSView, hidden, - NSView / . , NSView , NSView.

NSView hidden:YES hidden:NO. , - , NSView self.button:

...
[self.button setHidden:YES];
[self.button setFrameSize:NSZeroSize];
...

:

...
[self.button setFrameSize:NSMakeSize(160, 90)];
[self.button setHidden:NO];
...

, /, NSView ( , Interface Builder), :

Unable to simultaneously satisfy constraints:
(
    "<NSLayoutConstraint:0x608000082990 H:[NSButton:0x6080001200a0(100)]>",
    "<NSAutoresizingMaskLayoutConstraint:0x60800008bae0 h=--& v=--& H:[NSButton:0x6080001200a0(0)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x608000082990 H:[NSButton:0x6080001200a0(100)]>

, , = & le;, setTranslatesAutoresizingMaskIntoConstraints:NO NSView YES NSView:

...
[self.button setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.button setHidden:YES];
...

NSView:

...
[self.button setTranslatesAutoresizingMaskIntoConstraints:YES];
[self.button setHidden:NO];
...

NSView setHidden: ( !):

- (void)setHidden:(BOOL)hidden {
    [self setTranslatesAutoresizingMaskIntoConstraints:!hidden];
    [super setHidden:hidden];
}

[self.button setHidden:YES]; [self.button setHidden:NO];, .

+3

awakeFromNib:

, , , . awakeFromNib. , awakeFromNib , , . , . - .

100%,

[self.button setFrame:NSMakeRect(50, 10, 100, 100)];

- (void)viewDidLoad

method. Also, you should not forget to call super methods - in some cases this can be critical. So your final code should look like this:

@implementation AppDelegate

- (void)awakeFromNib{
    [super awakeFromNib];
    ... non GUI initialization
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.button setFrame:NSMakeRect(50, 10, 100, 100)];
}

@end

UPDATE: Thanks for the call stuck. Your opinion seems to be autoresist. You should check autoresizingMask and auto-layout restrictions

+2
source

All Articles