gpt4 book ai didi

c# - UISearchBar 中的中心 UISegmentedControl

转载 作者:行者123 更新时间:2023-11-29 03:55:02 25 4
gpt4 key购买 nike

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

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

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

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

最佳答案

您是通过 IB 还是以编程方式添加此内容?在 IB 中,我必须关闭“自动调整 subview 大小”并通过代码动态调整控件的大小。我将需要调整大小的控件放入可以绑定(bind)的 View 中,然后将控件置于该 View 中。这是一个示例。我在横向模式下并排放置了 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;

关于c# - UISearchBar 中的中心 UISegmentedControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16610834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com