Ios 快速扩展导航栏高度并添加分段控制
我设计了以下UINavigationBar,但是我有点困惑是否可以添加下面这样的分段控件?实现这一目标的最佳方式是什么Ios 快速扩展导航栏高度并添加分段控制,ios,swift,Ios,Swift,我设计了以下UINavigationBar,但是我有点困惑是否可以添加下面这样的分段控件?实现这一目标的最佳方式是什么 您可以按如下方式模拟您的需求: -(void) segmentAction: (UISegmentedControl *) segmentedControl { // Update the label with the segment number NSString *segmentNumber = [NSString stringWithFormat:@"%0
您可以按如下方式模拟您的需求:
-(void) segmentAction: (UISegmentedControl *) segmentedControl
{
// Update the label with the segment number
NSString *segmentNumber = [NSString stringWithFormat:@"%0d",
segmentedControl.selectedSegmentIndex + 1];
[(UITextView *)self.view setText:segmentNumber];
}
- (void) loadView
{
[super loadView];
// Create a central text view
UITextView *textView = [[UITextView alloc]
initWithFrame:self.view.frame];
textView.font = [UIFont fontWithName:@"Futura" size:96.0f];
textView.textAlignment = UITextAlignmentCenter;
self.view = textView;
// Create the segmented control
NSArray *buttonNames = [NSArray arrayWithObjects:
@"Recent", @"Popular", @"My", nil];
UISegmentedControl* segmentedControl = [[UISegmentedControl alloc]
initWithItems:buttonNames];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;
[segmentedControl addTarget:self action:@selector(segmentAction:)
forControlEvents:UIControlEventValueChanged];
// Add it to the navigation bar
self.navigationItem.titleView = segmentedControl;
}
或者还有另一种方法,如下所示:
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[NSString stringWithString:NSLocalizedString(@"Recent", @"")],
[NSString stringWithString:NSLocalizedString(@"Popular", @"")],
[NSString stringWithString:NSLocalizedString(@"My", @"")],
nil]];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.tintColor = [UIColor clearColor];
[segmentedControl setSelectedSegmentIndex:0];
[segmentedControl addTarget:self action:@selector(changeSegment:)
forControlEvents:UIControlEventValueChanged];
[segmentedControl setFrame:[self.navigationController.toolbar bounds]];
[self.navigationController.toolbar addSubview:segmentedControl];
导航条的延伸。(对不起,没有回答彼得的问题。) 在Xcode中,File-New-Swift File-Name类
import UIKit
import Foundation
extension UINavigationController
{
func makeBlackNavigationbar (){
print("black navigation")
navigationController?.navigationBarHidden = false
}
}
extension UINavigationBar
{
func makeBlackNavigationBar ()
{
barTintColor = UIColor.blackColor()
let titleDict: NSDictionary = [NSForegroundColorAttributeName: UIColor.whiteColor()]
titleTextAttributes = titleDict as [NSObject : AnyObject]
}
}
它可以在项目的任何地方实现
self.navigationController?.makeBlackNavigationbar()
self.navigationController?.navigationBar.makeBlackNavigationBar()
这适用于Swift 4 func makeBlackNavigationBar(){barTintColor=UIColor.black let titleDict:NSDictionary=[NSAttributedString.Key.foregroundColor:UIColor.marine]titlextAttributes=titleDict as[NSObject:AnyObject]as?[NSAttributedString.Key:Any]}