English 中文(简体)
ios 水平水平滚动菜单 - 背景按钮选择
原标题:ios horizontal scroll menu - background buttons select

我试图在滚动视图中绘制水平导航菜单。 对于菜单中的每一项目, 我有一个不同的图像。 当我选择一个项目时, 我想更改此项目的背景图像, 但是我不知道为什么我的代码不起作用 :

for (int i=0; i<12; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            UIImage *normal=[UIImage imageNamed:@"icon1"];
            UIImage *selec=[UIImage imageNamed:@"icon1down"];
            [btn setFrame:CGRectMake(i*larg, 0, 42.0, 43.0)];
            [btn setBackgroundImage:normal forState:UIControlStateNormal];
            [btn setBackgroundImage:selec forState:UIControlStateSelected];

            [scrollCat addSubview:btn];
            cx+=42.0;
    }
    [scrollCat setContentSize:CGSizeMake(cx, 43)];

代码里有坏东西吗?

问题回答

Try UIControlStateHighlighted instead of UIControlStateSelected. Highlighted is the state when the user touches your button. Selected is only used for some controls, like segments ; a custom button (one that say, stays selected) would/could make use of Selected.

UI control_Class docs at developers.appl.com (强调地雷):

<强度 > UI 控制国光线

Highlighted state of a control. A control enters this state when a touch enters and exits during tracking and when there is a touch up event. ...

<强度 > 联合情报控制国

Selected state of a control. For many controls, this state has no effect on behavior or appearance. But other subclasses (for example, the UISegmentedControl class) may have different appearance depending on their selected state. ...





相关问题
How to change out-of-focus text selection color in Xcode?

Okay, I ll bite. I ve got really pleasant code/window colors set up in Xcode. Ordinarily, my selection color is very visible. When I am doing a project search and iterating through the results, ...

Iphone NSTimer Issue

Hi I am new to objective c. I am trying to make an app for iphone. I have a button on my view, and the click on which the function playSound is called. This is working properly. It does plays the ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Xcode open two editor windows with same file

Is it possible to open the same file in two separate windows in Xcode. I can open a file in one window and the same file in the main Xcode editor window, but I wanted two separate fulltime editor ...

Forcing code signing refresh in Xcode

In our environment, we share resources across multiple projects and platforms. When building for iPhone, only a subset of those resources are needed. Since that subset is still considerable, we have ...

热门标签