У меня есть проект Cocoa, использующий Autolayout, и я хочу поместить кнопку (на самом деле NSPathControl
) над NSScrollView
, чтобы она оставалась на том же месте на экране, даже когда прокручивается scrollView.
Всякий раз, когда я добавляю кнопку к родительскому элементу scrollView, она оказывается позади scrollView (даже если я явно использую addSubview:positioned:relativeTo:
. Как мне заставить ее плавать над scrollView?
Мой запасной вариант - поместить его внутрь scrollView, включить translatesAutoresizingMaskIntoConstraints
и обновить фрейм по мере прокрутки scrollView. Я бы предпочел использовать autolayout, если это возможно...
Изменить: вот код макета для кнопки (макет содержимого в scrollView довольно сложный):
NSButton *button = [[NSButton alloc]initWithFrame:NSZeroRect];
button.translatesAutoresizingMaskIntoConstraints = NO;
button.wantsLayer = YES;//Added incase this affects ordering (it doesn't seem to make a difference)
[self.superview addSubview:button positioned:NSWindowAbove relativeTo:self];
[self.superview addConstraint:[NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:20]];
[self.superview addConstraint:[NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:20]];