0

我让我的窗口自动调整到可见屏幕。该代码没有问题。

但问题出在这里我需要将 ImageViews 添加到窗口中,当然它们也应该能够调整大小。

创建 ImageView 并使窗口适合当前可见的代码

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    int x,y;
    NSInteger rowCounter,gapX,gapY;
    rowNumber=5;
    rowCounter=0;
    gapX=0;
    gapY=0;
    personNumber=10;
    startX=20;
    startY=20;
    NSRect rect;
    NSSize size;
    NSScreen *scren=[NSScreen mainScreen];
    rect=scren.visibleFrame;
    y=rect.size.height;
    x=rect.size.width;
    NSLog(@"%s%s%d%s%d","screen"," y size=",y," x size=",x);
    y=window.frame.size.height;
    x=window.frame.size.width;
    NSLog(@"%s%s%d%s%d"," window"," y size=",y," x size=",x);
    timer1 =[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(resizeWindow1) userInfo:nil repeats:YES];
    rahmen=[[NSMutableArray alloc] initWithCapacity:25];
    NSPoint p;
    y=rect.size.height;
    x=rect.size.width;
    size.height=y;
    size.width=x;
    p.x= rect.origin.x;
    p.y= rect.origin.y+y;
    [window setContentSize:size];
    [window setFrameTopLeftPoint:p];
    width =x-(20*(personNumber/2));
    width=(width/(personNumber/2))-(20/(personNumber/2));
    NSLog(@"%s%ld","\n width: ",(long)width);

    high= y-(20*rowNumber);
    high=high/rowNumber-(20/rowNumber);
    NSLog(@"%s%ld","\n high: ",(long)high);
    for (indexRahmen=0; indexRahmen<25; ) {

        if (indexRahmen>0 && indexRahmen%(personNumber/2)==0) {
            rowCounter=rowCounter+1;
            gapX=0;
            gapY=gapY+1;
        }
        else if (indexRahmen%(personNumber/2)!=0){
            gapX=gapX+1;
        }
        if (rowCounter==rowNumber) {
            rowCounterr=rowNumber-1;
            gapY=gapY-1;
        }

        rect=NSMakeRect(startX+(width*(indexRahmen%(personNumber/2)))+(gapX*20),(startY+(rowCounter*high)+(gapY*20)),width, high);

        NSImageView *imageView=[[NSImageView alloc] initWithFrame:rect];
        imageView.image = [NSImage imageNamed:@"umrandung.png"];
        [rahmen insertObject:imageView atIndex:indexRahmen];
        [window1view.self addSubview:imageView];
        NSLog(@"%s%ld","\n IndexZahl: ",(long)indexRahmen);
        indexRahmen=indexRahmen+1;
    }

}

timer1 调用的调整大小操作几乎相同,除了:

NSImageView *imageView= [rahmen objectAtIndex:indexRahmen];
    imageView.frame= rect;
    imageView.image = [NSImage imageNamed:@"umrandung.png"];
    [rahmen insertObject:imageView atIndex:indexRahmen];

并且该 imageView 未添加到 window1view 的子视图中

确实发生的奇怪事情是 1. NSImageView 消失了,但是当我检查坐标时它们在正确的位置,所以我不知道它为什么会消失。

4

1 回答 1

0

我发现自己如何解决它。

我使用而不是使用以下方法创建 ImageView:

[rahmen objectAtIndex:indexRahmen];

我用了:

for (NSImageView* ImageView in rahmen)

它可以正常工作。

可悲的是,我不知道它用其他方法消失的原因

于 2013-05-26T10:24:46.853 回答