Adaptation of Uitabbar and Uinavigationbar in OC and Swift [Uitabbar in ipad]

Source: Internet
Author: User
Tags uikit

Author Sundays http://www.cnblogs.com/sundaysgarden/

Uitabbar adaptation in OC [Iphonex and ipad adaptation] customizing to Uitabar custom Uitabar header files

#import <UIKit/UIKit.h>

@interface Mctabbar:uitabbar

@property (nonatomic, strong) UIButton *centerbtn; Middle button

@end

customizing Uitabar m files

#import "GBArcView.h"

#import ""

#define SCREEN_WIDTH [[UIScreen Mainscreen] Bounds].size.width

#define Gbviewscall 1.2

#define CENTERBUTTONSCALL 1.1

@interface Mctabbar ()

@property (Nonatomic,strong) Gbarcview *gbview; semicircle View

Index of the child view of the @property (assign,nonatomic) int Index;//uitabbar

@end

@implementation Mctabbar

The Reinitialization method, which is loaded from Stroyboard, invokes the

-(Instancetype) Initwithcoder: (Nscoder *) adecoder{

if (self = [Super Initwithcoder:adecoder]) {

Self.backgroundcolor=[uicolor Whitecolor];

self.clipstobounds=no;//do not crop child controls

self.selecteditem=0;//Initializing indexes

Set the text color of Tabbaritem

[[Uitabbaritem appearance] Settitletextattributes:[nsdictionary Dictionarywithobjectsandkeys:rgb_color (74, 74, 7 4), Uitextattributetextcolor, Nil] forstate:uicontrolstatenormal];

//

[[Uitabbaritem appearance] Settitletextattributes: [NSDi Ctionary dictionarywithobjectsandkeys:rgb_color (0, 147, 197), Uitextattributetextcolor, Nil]forState: Uicontrolstateselected];

}

return self;

}

-(Instancetype) initWithFrame: (CGRect) frame{

if (self = [Super Initwithframe:frame]) {

[Self initview];

}

return self;

}

-(void) initview{

[Self addsubview:_centerbtn];

[Self Insertsubview:_gbview belowsubview:_centerbtn];

}

Handling out-of-area click Invalid issues

-(UIView *) HitTest: (cgpoint) point withevent: (Uievent *) event{

if (Self.hidden) {

return [Super Hittest:point withevent:event];

}else {

Convert coordinates

Cgpoint temppoint = [self.centerbtn convertpoint:point fromview:self];

Determine if the clicked point is within the button area

if (Cgrectcontainspoint (Self.centerBtn.bounds, temppoint)) {

_centerbtn.selected = YES;

Back button

return _centerbtn;

}else {

_centerbtn.selected = NO;

__weak __typeof (&*self) weakself =self;

return [Super Hittest:point withevent:event];

}

}

}

Draw horizontal Lines

-(void) DrawRect: (cgrect) Rect {

The middle button width is the height of the Uitabbar, the width of the other buttons is, (self.width-self.height)/4.0

CGFloat buttonw = (self.width-self.height)/4.0;

Cgcontextref context = Uigraphicsgetcurrentcontext ();

Cgcontextsetstrokecolorwithcolor (context, [Uicolor colorwithred:200/255.0 green:200/255.0 blue:200/255.0 alpha:0.8] . Cgcolor);

Cgcontextsetlinewidth (context, single_line_width + 2.0f);

Cgcontextsetlinewidth (context, 5);

Cgcontextbeginpath (context);

CGFloat linemargin = 0;

1PX line, pixel offset

CGFloat pixeladjustoffset = 0;

if (((int) (1 * [UIScreen Mainscreen].scale) + 1)% 2 = = 0) {

Pixeladjustoffset = Single_line_adjust_offset;

}

CGFloat yPos = Linemargin-pixeladjustoffset;

First paragraph line

Cgcontextmovetopoint (context, 0, YPos);

Cgcontextaddlinetopoint (context, buttonw*2+single_line_width*2, YPos);

Cgcontextstrokepath (context);

Second segment line

Cgcontextmovetopoint (context, buttonw*2+self.frame.size.height-single_line_width*2, YPos);

Cgcontextaddlinetopoint (context, self.bounds.size.width, YPos);

Cgcontextsetstrokecolorwithcolor (context, [Uicolor colorwithred:200/255.0 green:200/255.0 blue:200/255.0 alpha:0.8] . Cgcolor);

Cgcontextstrokepath (context);

}

Lazy Loading of custom buttons

-(UIButton *) centerbtn{

if (!_CENTERBTN) {

_CENTERBTN = [UIButton buttonwithtype:uibuttontypecustom];

Set button size to fit picture

UIImage *normalimage = [UIImage imagenamed:@ "3_gray"];

_centerbtn.frame = CGRectMake (0, 0, normalImage.size.width, normalImage.size.height);

[_centerbtn setimage:normalimage Forstate:uicontrolstatenormal];

UIImage *selectimage = [UIImage imagenamed:@ "3_hover"];

[_centerbtn setimage:selectimage forstate:uicontrolstateselected];

Highlight when removing selection

_centerbtn.adjustsimagewhenhighlighted = NO;

Adjust the position of the button according to the picture (the center of the picture in the middle of the tabbar, this time because the button is a part of the Tabbar, so click Invalid, to be processed)

_centerbtn.frame = CGRectMake (([UIScreen mainscreen].bounds.size.width-normalimage.size.width)/2.0,- normalimage.size.height/2.0 + 8, normalImage.size.width, normalImage.size.height);

_centerbtn.transform = Cgaffinetransformmakescale (Centerbuttonscall, Centerbuttonscall);

}

return _centerbtn;

}

Lazy Loading of custom semicircle view

-(UIView *) gbview{

if (!_gbview) {

CGFloat buttonw = self.centerBtn.width;

Gbarcview *gbview = [[Gbarcview alloc]initwithframe:cgrectmake (0,0,buttonw *gbviewscall,buttonw*gbviewscall)];

Gbview.backgroundcolor=[uicolor Whitecolor];

Gbview.layer.maskstobounds=yes;

gbview.layer.cornerradius=buttonw*gbviewscall*0.5f;

Gbview.center = _centerbtn.center;

Gbview.transform = Cgaffinetransformmakescale (Gbviewscall, Gbviewscall);

_gbview = Gbview;

}

return _gbview;

}

-(void) Sethidden: (BOOL) hidden{

[Super Sethidden:hidden];

When manually setting Uitabbar hide, we want to hide custom buttons and backgrounds

[Self.gbview Sethidden:hidden];

[Self.centerbtn Sethidden:hidden];

}

Core part ******

When you configure hidesbottombarwhenpushed Viewcontroller, hide Uitabbar, the frame is changed, and the Y value of Uitabbar is set to the maximum Y value of the screen, it is not visible. We rewrite this method to determine that when frame y is smaller than the screen height, then the Uitabbar is hidden, and we hide the custom controls. Instead, we'll show our custom controls.

-(void) Setframe: (CGRect) frame{

if (Self.superview &&cgrectgetmaxy (self.superview.bounds)!=cgrectgetmaxy (frame)) {

FRAME.ORIGIN.Y =cgrectgetheight (self.superview.bounds)-cgrectgetheight (frame);

//    }

[Super Setframe:frame];

if (Frame.origin.y>=[uiscreen mainscreen].bounds.size.height) {

[Self.gbview Sethidden:yes];

[Self.centerbtn Sethidden:yes];

}else{

[Self.gbview Sethidden:no];

[Self.centerbtn Sethidden:no];

}

}

Adaptation ipad//rewrite the Traitcollection method so that the Uitabbar remains vertically arranged to prevent horizontal arrangement in Ipad11

-(Uitraitcollection *) traitcollection {

if (UIDevice.currentDevice.userInterfaceIdiom = = Uiuserinterfaceidiompad) {

return [Uitraitcollection traitcollectionwithverticalsizeclass:uiuserinterfacesizeclasscompact];

}

return [Super Traitcollection];

}

@end

Setting the adapter Iphonex in a uitabbarcontroller.m file

Use KVC to assign your own tabbar to the system Tabbar

Add the following code to the Viewdidload method

Copy the custom file to the system using KVC

[Self Setvalue:_mctabbar forkeypath:@ "TabBar"];

Add a view setting Uitabbar height to the [custom] Uitabbar of the system

[Self setuptabbar];

-(void) setuptabbar{

To delete an existing Tabbar

CGRect rect = self.tabBar.frame;

[Self.tabbar Removefromsuperview]; Remove the lower bar from the Tabbarcontroller

[Self.tabBarController.tabBar Removefromsuperview];

Rect.size.height = 49;

RECT.ORIGIN.Y = [UIScreen mainscreen].bounds.size.height-49;

UIView *myview = [[UIView alloc] init];

Myview.frame = rect;

Myview.backgroundcolor = [Uicolor Whitecolor];

[Self.view Addsubview:myview];

}

Customizing files referenced in the Uitabbar

GBArcView.h

#define SINGLE_LINE_WIDTH (1/[UIScreen Mainscreen].scale)

#define SINGLE_LINE_ADJUST_OFFSET ((1/[UIScreen Mainscreen].scale)/2)

@interface Gbarcview:uiview

@end

Gbarch.m

#import <QuartzCore/QuartzCore.h>

#import "MATH.H"

#import <CoreGraphics/CoreGraphics.h>

@implementation Gbarcview

-(void) DrawRect: (cgrect) Rect {

Cgcontextref context = Uigraphicsgetcurrentcontext ();

Uicolor *color1 = [Uicolor colorwithred:200/255.0 green:200/255.0 blue:200/255.0 alpha:0.8];

Cgcontextsetstrokecolorwithcolor (context, Color1. Cgcolor);

Cgcontextsetlinewidth (context, single_line_width + 0.5f);

Cgcontextbeginpath (context);

CGFloat Linemargin =self.frame.size.width*0.5f;

1px line, offset pixel point

CGFloat pixeladjustoffset = 0;

if (((int) (1 * [UIScreen Mainscreen].scale) + 1)% 2 = = 0) {

Pixeladjustoffset = Single_line_adjust_offset;

}

CGFloat yPos = Self.frame.size.width*0.5f-pixeladjustoffset;

CGFloat xPos = self.frame.size.width*0.5f-pixeladjustoffset-4.0f;

Cgcontextaddarc (context, XPos, YPos, Self.frame.size.width*0.5-1.f, m_pi*1.08, m_pi*1.95, 0);

Cgcontextdrawpath (context, kcgpathstroke);

Cgcontextstrokepath (context);

}

@end

Fitting Iphonex

Defining the classification implementation of Uitabar

The Tabbaritem icons and files are still listed on the ipad in Swift

Http://www.cnblogs.com/sundaysgarden/articles/9044340.html

Adaptation of Uitabbar and Uinavigationbar in OC and Swift [Uitabbar in ipad]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.