UIView always top subview [duplicate]

2019-07-09 12:52发布

This question already has an answer here:

Hi I have a game where I create a subview that handles touch events. But when I add other objects later in the code, they appear in front of that view. So if the user tap the screen on one of these objects the touch won't be registered. So is there a way to make a UIView always appear on the top? I have tried this, but it doesn't work: view.layer.zPosition = MAXFLOAT;

Any ideas? Thanks

3条回答
贪生不怕死
2楼-- · 2019-07-09 13:35
[[self view] bringSubviewToFront:yourView];
查看更多
看我几分像从前
3楼-- · 2019-07-09 13:36

If for some reason you don't want to keep a pointer to the subview as others have suggested you can add subviews using this method:

- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index

with index = 0. This will insert each new subview under the previous. I would personally probably still do what @Alexander is doing in his answer/comments.

查看更多
Bombasti
4楼-- · 2019-07-09 13:41

Try with bringSubviewToFront:, did the trick for me:

[[self view] bringSubviewToFront:yourView];
查看更多
登录 后发表回答