Không có "hành vi điền không gian" cho UIViews nói chung. Họ nhận được kích thước họ được giao. Tất cả các bạn có thể làm là:
- thiết lập mặt nạ autoresizing của họ để kiểm soát cách thức chúng được thay đổi kích cỡ nếu xem cha mẹ của họ thay đổi kích thước của nó
- thiết UIViewContentMode của họ để kiểm soát cách họ thay đổi kích thước nội dung của họ (quan trọng đối với UIImageViews, cho ví dụ)
Trong trường hợp của bạn, bạn có thể làm như sau để có được một UIToolbar chứa một UISegmentedControl đó là rộng như thanh công cụ:
(void)viewDidLoad
{
[super viewDidLoad];
// Create the toolbar; place it at the bottom of the view.
UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height-44, self.view.bounds.size.width, 44)];
myToolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
[self.view addSubview:myToolbar];
// Create the UISegmentedControl with two segments and "Bar" style. Set frame size to that of the toolbar minus 6pt margin on both sides, since 6pt is the padding that is enforced anyway by the UIToolbar.
UISegmentedControl *mySegmentedControl = [[UISegmentedControl alloc] initWithFrame:CGRectInset(myToolbar.frame, 6, 6)];
// Set autoresizing of the UISegmentedControl to stretch horizontally.
mySegmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
[mySegmentedControl insertSegmentWithTitle:@"First" atIndex:0 animated:NO];
[mySegmentedControl insertSegmentWithTitle:@"Second" atIndex:1 animated:NO];
mySegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
// Create UIBarButtonItem with the UISegmentedControl as custom view, and add it to the toolbar's items
UIBarButtonItem *myBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:mySegmentedControl];
myToolbar.items = [NSArray arrayWithObject:myBarButtonItem];
}
Nguồn
2012-05-09 11:38:19