带有 UISegmentedControl AutoLayout 的 UIToolbar(/全宽)
UIToolbar with UISegmentedControl AutoLayout (/full width)
我有一个 UIViewController
,在 UINavigationBar
下面有一个 UIToolBar
,在下面有一个 UITableView
。我的 UIToolBar
上唯一的 UIBarButtonItem
是 UISegmentedControl
,如下面的屏幕截图所示:
但是,如何让 UISegmentedControl
/UIBarButtonItem
像在 App Store 应用程序中那样拉伸以填充 UIToolBar
?
请记住,我还支持横向模式,因此当设备旋转时它必须自动拉伸以适应新的宽度。这是我希望用 AutoLayout
实现的,但据我所知我不能在 UIToolBar
中使用 AutoLayout
。
有什么建议(除了用 UIView
替换 UIToolbar
之外)吗?
我创建了一个工具栏,里面有一个分段控件,然后将分段控件设置为 320 宽。然后,我在每一侧都设置了灵活的布局指南,以强制将分段控件置于中心。这在纵向上看起来不错,但分段控件在横向上不会伸展。
纵向(包括 IB 设置和模拟器)
横向(还包括 IB 设置和模拟器)
应用商店似乎有一个分段控件,它被设置为一定的宽度,然后视图不允许旋转。我知道您希望分段控件随布局改变宽度,但不幸的是,目前自动布局无法做到这一点。
我的建议是,如果您真的希望分段控件随旋转改变宽度,则可以在设备旋转到横向时以编程方式设置宽度。
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
{
//set segmented control to landscape
}
if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
{
//set segmented control to portrait width
}
}
还有一件事,如果你最终使用了这个设备旋转的东西,你仍然需要 IB 中的那些灵活的垫片来保持你的分段控件在工具栏中居中
你可以用代码做到这一点。是这样的:
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UIToolbar *toolbar;
@property (strong, nonatomic) IBOutlet UISegmentedControl *segment;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_segment.translatesAutoresizingMaskIntoConstraints = NO;
[_toolbar addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-8-[_segment]-8-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_segment)]];
[_toolbar addConstraint:[NSLayoutConstraint constraintWithItem:_segment
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:_toolbar
attribute:NSLayoutAttributeCenterY
multiplier:1.
constant:0.]];
}
@end
我有一个 UIViewController
,在 UINavigationBar
下面有一个 UIToolBar
,在下面有一个 UITableView
。我的 UIToolBar
上唯一的 UIBarButtonItem
是 UISegmentedControl
,如下面的屏幕截图所示:
但是,如何让 UISegmentedControl
/UIBarButtonItem
像在 App Store 应用程序中那样拉伸以填充 UIToolBar
?
请记住,我还支持横向模式,因此当设备旋转时它必须自动拉伸以适应新的宽度。这是我希望用 AutoLayout
实现的,但据我所知我不能在 UIToolBar
中使用 AutoLayout
。
有什么建议(除了用 UIView
替换 UIToolbar
之外)吗?
我创建了一个工具栏,里面有一个分段控件,然后将分段控件设置为 320 宽。然后,我在每一侧都设置了灵活的布局指南,以强制将分段控件置于中心。这在纵向上看起来不错,但分段控件在横向上不会伸展。
纵向(包括 IB 设置和模拟器)
横向(还包括 IB 设置和模拟器)
应用商店似乎有一个分段控件,它被设置为一定的宽度,然后视图不允许旋转。我知道您希望分段控件随布局改变宽度,但不幸的是,目前自动布局无法做到这一点。
我的建议是,如果您真的希望分段控件随旋转改变宽度,则可以在设备旋转到横向时以编程方式设置宽度。
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
{
//set segmented control to landscape
}
if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
{
//set segmented control to portrait width
}
}
还有一件事,如果你最终使用了这个设备旋转的东西,你仍然需要 IB 中的那些灵活的垫片来保持你的分段控件在工具栏中居中
你可以用代码做到这一点。是这样的:
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UIToolbar *toolbar;
@property (strong, nonatomic) IBOutlet UISegmentedControl *segment;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_segment.translatesAutoresizingMaskIntoConstraints = NO;
[_toolbar addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-8-[_segment]-8-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_segment)]];
[_toolbar addConstraint:[NSLayoutConstraint constraintWithItem:_segment
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:_toolbar
attribute:NSLayoutAttributeCenterY
multiplier:1.
constant:0.]];
}
@end