1

嗨,我是 ios 开发的新手。我正在尝试将标签从视图拖到工具栏...但是标签将位于工具栏的背面。我希望将标签拖到工具栏的前面...请就这个问题帮助我..... .这是我到目前为止所做的代码

- (void)viewDidLoad {
    [super viewDidLoad];

    UIToolbar *backgrdtoolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,44)];

    [backgrdtoolbar setBarStyle:UIBarStyleBlackOpaque];

    [self.view addSubview:backgrdtoolbar];

    mainScroll =  [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 45)];

    [mainScroll setContentSize:CGSizeMake(600,45)];
    [mainScroll setBackgroundColor:[UIColor clearColor]];
    [mainScroll setShowsHorizontalScrollIndicator:NO];

    mainToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 600, 44)];
    [mainToolbar setBarStyle:UIBarStyleBlackTranslucent];

    [self.view addSubview:mainScroll];

    [mainScroll addSubview:mainToolbar];

    UIBarButtonItem *b1 = [[UIBarButtonItem alloc]initWithTitle:@"1" style:UIBarButtonItemStyleBordered target:self action:@selector(_1:)];
    UIBarButtonItem *b2 = [[UIBarButtonItem alloc]initWithTitle:@"2" style:UIBarButtonItemStyleBordered target:self action:@selector(_1:)];
    UIBarButtonItem *b3 = [[UIBarButtonItem alloc]initWithTitle:@"3" style:UIBarButtonItemStyleBordered target:self action:@selector(_1:)];

    UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

    NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:b1,spacer,b2,spacer,b3, spacer,b4,spacer,b5,spacer,b6,spacer,b7,spacer,b8,spacer,b9,spacer,b10,nil];
    [mainToolbar setItems:arr];

}



 - (IBAction)handle:(UIPanGestureRecognizer *)gesture {
        static CGRect originalFrame;

        if (gesture.state == UIGestureRecognizerStateBegan){
            originalFrame = gesture.view.frame;
        } else if (gesture.state == UIGestureRecognizerStateChanged) {
            CGPoint translate = [gesture translationInView:gesture.view.superview];

            CGRect newFrame = CGRectMake(fmin(gesture.view.superview.frame.size.width - originalFrame.size.width, fmax(originalFrame.origin.x + translate.x, 0.0)),
                                         fmin(gesture.view.superview.frame.size.height - originalFrame.size.height, fmax(originalFrame.origin.y + translate.y , 0.0)),
                                         originalFrame.size.width,
                                         originalFrame.size.height);


            gesture.view.frame = newFrame;

        }

    }
4

1 回答 1

0

是否可以将一个视图拖到另一个视图?对的,这是可能的。

但是,如果对您来说唯一重要的是将标签放在另一个视图之前,我认为最好的方法是不要更改您的标签所在的视图。我的意思是您不需要更改其他视图,位于您的目标视图内。

如果我站在你的立场上,我将使用一个视图,该视图具有放置标签的视图以包含两个视图,即放置标签的视图和可以拖动标签的视图。

当您将标签添加到此视图时,要注意的重要一点是它必须在您的视图前面,您可以使用以下代码来做到这一点:

[view bringSubviewToFront:yourLabel];

之后,您可以将它拖到您的视图容器中,它将位于其他视图的前面。:)

-

如果您真的想将它从一个视图更改为另一个视图,则必须将其从原始视图中删除,并将其添加到将放置的另一个视图中。类似的东西

if(gesture.view.frame.origin.y > y ) {
    [gesture.view removeFromSuperview];
    [newView addSubview:gesture.view];
    //Your code to adjust the frame for the new view, because your frame is relative
    //It is define by your superview
}
于 2013-05-28T19:10:08.617 回答