Ответ 1
Ну, ответ прост и нигде не близок, как раздутый. Тем не менее, они не работают с увеличением двойного крана.
Это так. Это просто работает. Вы также можете настроить свои настройки по мере необходимости.
В @implementation вам нужно только выполнить переопределение constrainBoundsRect:
- (NSRect)constrainBoundsRect:(NSRect)proposedClipViewBoundsRect {
NSRect constrainedClipViewBoundsRect = [super constrainBoundsRect:proposedClipViewBoundsRect];
// Early out if you want to use the default NSClipView behavior.
if (self.centersDocumentView == NO) {
return constrainedClipViewBoundsRect;
}
NSRect documentViewFrameRect = [self.documentView frame];
// If proposed clip view bounds width is greater than document view frame width, center it horizontally.
if (proposedClipViewBoundsRect.size.width >= documentViewFrameRect.size.width) {
// Adjust the proposed origin.x
constrainedClipViewBoundsRect.origin.x = centeredCoordinateUnitWithProposedContentViewBoundsDimensionAndDocumentViewFrameDimension(proposedClipViewBoundsRect.size.width, documentViewFrameRect.size.width);
}
// If proposed clip view bounds is hight is greater than document view frame height, center it vertically.
if (proposedClipViewBoundsRect.size.height >= documentViewFrameRect.size.height) {
// Adjust the proposed origin.y
constrainedClipViewBoundsRect.origin.y = centeredCoordinateUnitWithProposedContentViewBoundsDimensionAndDocumentViewFrameDimension(proposedClipViewBoundsRect.size.height, documentViewFrameRect.size.height);
}
return constrainedClipViewBoundsRect;
}
CGFloat centeredCoordinateUnitWithProposedContentViewBoundsDimensionAndDocumentViewFrameDimension
(CGFloat proposedContentViewBoundsDimension,
CGFloat documentViewFrameDimension )
{
CGFloat result = floor( (proposedContentViewBoundsDimension - documentViewFrameDimension) / -2.0F );
return result;
}
В @interface просто добавьте одно единственное свойство. Это позволяет не использовать центрирование. Как вы можете себе представить, может быть условная логика, которую вы хотите постепенно центрировать.
@property BOOL centersDocumentView;
Кроме того, не забудьте установить этот BOOL
в YES
или NO
в своем переопределении
initWithFrame
и initWithCoder:
поэтому у вас будет известное значение по умолчанию, которое будет работать.
(помните, что дети, initWithCoder:
, позволяют вам делать необходимые действия и задавать класс просмотра в nib. Не забудьте вызвать супер до ваших вещей!)
Конечно, если вам нужно поддерживать что-либо раньше 10.9, вам нужно будет реализовать другой материал.
(хотя, вероятно, не так много, как другие...)