覆盖layoutSubviews以调用[super layoutSubviews],然后查找并重新定位按钮的视图

Eri*_*tto 9 iphone xcode cocoa-touch objective-c uinavigationbar

我正在尝试重新定位我的位置UIBarButtonItem,使其位于UINavigationBar顶部和右侧的边缘.我找到关于如何做到这一点的接受答案,但我真的不明白如何编码.

我开始创建一个名为CustomNavBar继承自的新类UINavigationBar.然后我将此方法放在实现中:

- (void) layoutSubviews
{


}
Run Code Online (Sandbox Code Playgroud)

我不明白的是答案中的部分

call [super layoutSubviews] and then find and reposition the button's view.
Run Code Online (Sandbox Code Playgroud)

我该如何编码呢?即使是正确方向的推动也会有所帮助.谢谢!

voi*_*ern 16

你打电话找到你想要移动的按钮,layoutSubviews因为它会被重新定位到每次layoutSubviews调用iOS时想要的位置.

- (void) layoutSubviews
{
    [super layoutSubviews];

    for (UIView *view in self.subviews) {   // Go through all subviews
        if (view == buttonYouWant) {  // Find the button you want
            view.frame = CGRectOffset(view.frame, 0, -5);   // Move it
        }
    }
}
Run Code Online (Sandbox Code Playgroud)