IOS-set the size of the button in the middle title Position of the Weibo navigation bar --- setFrame ----, frame of the buttons in the ios navigation bar
# Import "HWTitleButton. h"
# Define HWMargin 5
@ Implementation HWTitleButton
-(Id) initWithFrame :( CGRect) frame
{
Self = [super initWithFrame: frame];
If (self ){
[Self setTitleColor: [UIColor blackColor] forState: UIControlStateNormal];
Self. titleLabel. font = [UIFont boldSystemFontOfSize: 17];
[Self setImage: [UIImage imageNamed: @ "navigationbar_arrow_down"] forState: UIControlStateNormal];
[Self setImage: [UIImage imageNamed: @ "navigationbar_arrow_up"] forState: UIControlStateSelected];
}
Return self;
}
// Purpose: Modify the size after the system calculates and sets the button size.
/**
* Rewrite setFrame: Method purpose: intercept the process of setting button size
* If you want to modify the widget size after the system has set it, and make sure the modification is successful, it is generally set in setFrame:
*/
-(Void) setFrame :( CGRect) frame // to thoroughly modify the widget size, you must modify it based on setFrame.
{
Frame. size. width + = HWMargin; // Add HWMargin to the sent width; add it before [super setFrame: frame;
// Frame. origin. x + = 10;
// Frame. origin. y + = 20;
[Super setFrame: frame];
}
-(Void) layoutSubviews
{
[Super layoutSubviews];
// If you only want to adjust the positions of titleLabel and imageView in the button, set the positions separately in layoutSubviews.
// 1. Calculate the frame of titleLabel
Self. titleLabel. x = self. imageView. x;
// 2. Calculate the frame of the imageView. The Arrow picture is placed in the imageView.
Self. imageView. x = CGRectGetMaxX (self. titleLabel. frame) + HWMargin;
}
-(Void) setTitle :( NSString *) title forState :( UIControlState) state
{
[Super setTitle: title forState: state];
// As long as the text is modified, let the button recalculate its size
[Self sizeToFit];
}
-(Void) setImage :( UIImage *) image forState :( UIControlState) state
{
[Super setImage: image forState: state];
// As long as the image is modified, let the button recalculate its size
[Self sizeToFit];
}
@ End