0

我正在使用带有 a 的内置搜索范围UISearchDisplayController,并且我只有 2 个段。

问题是我们的设计需要按钮更小且居中(在 iPad 上看起来尤其糟糕,因为按钮被拉伸得很宽)。

有没有办法将其居中UISegmentedControl并使其更小?我已经UISegmentedControl通过循环拉出subViews。我可以用 设置每个段的宽度setWidth:forSegmentAtIndex,但控件停靠在左侧。我怎样才能把它居中?

PS - 我的应用程序是 MonoTouch (Xamarin.iOS),但欢迎使用 Obj-C 答案。

4

1 回答 1

1

您是通过 IB 还是以编程方式添加它?在 IB 中,我不得不关闭“Autoresize Subviews”并通过代码动态调整控件的大小。我将需要调整大小的控件放入一个可以绑定的视图中,然后在该视图中居中我的控件。这是一个示例。我有 2 个按钮,我在横向模式下并排放置,但它应该给你一个想法。

// get the current sizes of the things we are moving
CGRect saveRect = self.viewButtons.frame;  // the enclosing view
CGRect addLocRect = self.buttonAddLocation.frame;  // button 1
CGRect connectRect = self.buttonConnect.frame;     // button 2

// This will be set below in one of the if-else branches
CGFloat buttonWidth = 0;

// determine the offset from the left/right based on device and orientation
int offsetLeft = 0;
int offsetRight = 0;
if ([self isIphone]) {
    offsetLeft = (UIDeviceOrientationIsPortrait(toInterfaceOrientation)) ? OFFSET_LEFT_PORTRAIT_IPHONE : OFFSET_LEFT_LANDSCAPE_IPHONE;
    offsetRight = (UIDeviceOrientationIsPortrait(toInterfaceOrientation)) ? OFFSET_RIGHT_PORTRAIT_IPHONE : OFFSET_RIGHT_LANDSCAPE_IPHONE;

} else {
    offsetLeft = (UIDeviceOrientationIsPortrait(toInterfaceOrientation)) ? OFFSET_LEFT_PORTRAIT_IPAD : OFFSET_LEFT_LANDSCAPE_IPAD;
    offsetRight = offsetLeft;
}


// change the size & location of the buttons to maximize the area for the location list
// no matter what orientation, the button frame will fill the bottom of the screen
saveRect.size.width = _windowWidth -offsetLeft - offsetRight;

// for Landscape, move the buttons to side-by-side at the bottom of the window
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {

    // size & move the buttons to fit side-by-side
    buttonWidth = (saveRect.size.width)*.4;

    // addLocRect.origin.x += offset;
    addLocRect.origin.y = saveRect.size.height - addLocRect.size.height ;

    connectRect.origin.x = saveRect.size.width - buttonWidth - offsetRight;
    connectRect.origin.y = saveRect.size.height - connectRect.size.height;

} else { // Portrait

    // move the buttons down to the bottom of the frame, stacked
    // size the buttons to be fully across the screen
    buttonWidth = saveRect.size.width-2*offsetLeft;

    addLocRect.origin.y = 0 ; // at the top of the button view
    addLocRect.origin.x = offsetLeft;
    connectRect.origin.y = saveRect.size.height - connectRect.size.height;
    connectRect.origin.x = offsetLeft;

}

connectRect.size.width = buttonWidth;
addLocRect.size.width = buttonWidth;
self.buttonAddLocation.frame = addLocRect;
self.buttonConnect.frame = connectRect;
于 2013-05-17T14:27:31.100 回答